Skip to content

Commit 08e3b45

Browse files
committed
Fixed the chapter Dynamic Programming
1 parent c5d6375 commit 08e3b45

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

C++/chapDynamicProgramming.tex

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ \subsubsection{动规}
101101
// 时间复杂度O(n),空间复杂度O(1)
102102
class Solution {
103103
public:
104-
int maxSubArray(int A[], int n) {
104+
int maxSubArray(vector<int>& nums) {
105105
int result = INT_MIN, f = 0;
106-
for (int i = 0; i < n; ++i) {
107-
f = max(f + A[i], A[i]);
106+
for (int i = 0; i < nums.size(); ++i) {
107+
f = max(f + nums[i], nums[i]);
108108
result = max(result, f);
109109
}
110110
return result;
@@ -119,22 +119,24 @@ \subsubsection{思路5}
119119
// 时间复杂度O(n),空间复杂度O(n)
120120
class Solution {
121121
public:
122-
int maxSubArray(int A[], int n) {
123-
return mcss(A, n);
122+
int maxSubArray(vector<int>& A) {
123+
return mcss(A.begin(), A.end());
124124
}
125125
private:
126126
// 思路5,求最大连续子序列和
127-
static int mcss(int A[], int n) {
128-
int i, result, cur_min;
127+
template <typename Iter>
128+
static int mcss(Iter begin, Iter end) {
129+
int result, cur_min;
130+
const int n = distance(begin, end);
129131
int *sum = new int[n + 1]; // 前n项和
130132

131133
sum[0] = 0;
132134
result = INT_MIN;
133135
cur_min = sum[0];
134-
for (i = 1; i <= n; i++) {
135-
sum[i] = sum[i - 1] + A[i - 1];
136+
for (int i = 1; i <= n; i++) {
137+
sum[i] = sum[i - 1] + *(begin + i - 1);
136138
}
137-
for (i = 1; i <= n; i++) {
139+
for (int i = 1; i <= n; i++) {
138140
result = max(result, sum[i] - cur_min);
139141
cur_min = min(cur_min, sum[i]);
140142
}
@@ -191,7 +193,7 @@ \subsubsection{代码}
191193
// 时间复杂度O(n^2),空间复杂度O(n^2)
192194
class Solution {
193195
public:
194-
int minCut(string s) {
196+
int minCut(const string& s) {
195197
const int n = s.size();
196198
int f[n+1];
197199
bool p[n][n];
@@ -369,7 +371,7 @@ \subsubsection{递归}
369371
// 递归,会超时,仅用来帮助理解
370372
class Solution {
371373
public:
372-
bool isInterleave(string s1, string s2, string s3) {
374+
bool isInterleave(const string& s1, const string& s2, const string& s3) {
373375
if (s3.length() != s1.length() + s2.length())
374376
return false;
375377

@@ -400,7 +402,7 @@ \subsubsection{动规}
400402
// 二维动规,时间复杂度O(n^2),空间复杂度O(n^2)
401403
class Solution {
402404
public:
403-
bool isInterleave(string s1, string s2, string s3) {
405+
bool isInterleave(const string& s1, const string& s2, const string& s3) {
404406
if (s3.length() != s1.length() + s2.length())
405407
return false;
406408

@@ -430,7 +432,7 @@ \subsubsection{动规+滚动数组}
430432
// 二维动规+滚动数组,时间复杂度O(n^2),空间复杂度O(n)
431433
class Solution {
432434
public:
433-
bool isInterleave(string s1, string s2, string s3) {
435+
bool isInterleave(const string& s1, const string& s2, const string& s3) {
434436
if (s1.length() + s2.length() != s3.length())
435437
return false;
436438

@@ -528,12 +530,12 @@ \subsubsection{分析}
528530
\subsubsection{递归}
529531

530532
\begin{Code}
531-
// LeetCode, Interleaving String
533+
// LeetCode, Scramble String
532534
// 递归,会超时,仅用来帮助理解
533535
// 时间复杂度O(n^6),空间复杂度O(1)
534536
class Solution {
535537
public:
536-
bool isScramble(string s1, string s2) {
538+
bool isScramble(const string& s1, const string& s2) {
537539
return isScramble(s1.begin(), s1.end(), s2.begin());
538540
}
539541
private:
@@ -559,11 +561,11 @@ \subsubsection{递归}
559561

560562
\subsubsection{动规}
561563
\begin{Code}
562-
// LeetCode, Interleaving String
564+
// LeetCode, Scramble String
563565
// 动规,时间复杂度O(n^3),空间复杂度O(n^3)
564566
class Solution {
565567
public:
566-
bool isScramble(string s1, string s2) {
568+
bool isScramble(const string& s1, const string& s2) {
567569
const int N = s1.size();
568570
if (N != s2.size()) return false;
569571

@@ -597,16 +599,16 @@ \subsubsection{动规}
597599

598600
\subsubsection{递归+剪枝}
599601
\begin{Code}
600-
// LeetCode, Interleaving String
602+
// LeetCode, Scramble String
601603
// 递归+剪枝
602604
// 时间复杂度O(n^6),空间复杂度O(1)
603605
class Solution {
604606
public:
605-
bool isScramble(string s1, string s2) {
607+
bool isScramble(const string& s1, const string& s2) {
606608
return isScramble(s1.begin(), s1.end(), s2.begin());
607609
}
608610
private:
609-
typedef string::iterator Iterator;
611+
typedef string::const_iterator Iterator;
610612
bool isScramble(Iterator first1, Iterator last1, Iterator first2) {
611613
auto length = distance(first1, last1);
612614
auto last2 = next(first2, length);
@@ -634,12 +636,12 @@ \subsubsection{递归+剪枝}
634636

635637
\subsubsection{备忘录法}
636638
\begin{Code}
637-
// LeetCode, Interleaving String
639+
// LeetCode, Scramble String
638640
// 递归+map做cache
639-
// 时间复杂度O(n^3),空间复杂度O(n^3)
641+
// 时间复杂度O(n^3),空间复杂度O(n^3), TLE
640642
class Solution {
641643
public:
642-
bool isScramble(string s1, string s2) {
644+
bool isScramble(const string& s1, const string& s2) {
643645
cache.clear();
644646
return isScramble(s1.begin(), s1.end(), s2.begin());
645647
}
@@ -694,14 +696,14 @@ \subsubsection{备忘录法}
694696
};
695697
}
696698

697-
// LeetCode, Interleaving String
699+
// LeetCode, Scramble String
698700
// 递归+unordered_map做cache,比map快
699701
// 时间复杂度O(n^3),空间复杂度O(n^3)
700702
class Solution {
701703
public:
702704
unordered_map<Key, bool> cache;
703705

704-
bool isScramble(string s1, string s2) {
706+
bool isScramble(const string& s1, const string& s2) {
705707
cache.clear();
706708
return isScramble(s1.begin(), s1.end(), s2.begin());
707709
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy