Skip to content

Commit c5d6375

Browse files
committed
Fixed the chapter Greedy
1 parent ffc6829 commit c5d6375

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

C++/chapGreedy.tex

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ \subsubsection{代码1}
3838
// 思路1,时间复杂度O(n),空间复杂度O(1)
3939
class Solution {
4040
public:
41-
bool canJump(int A[], int n) {
41+
bool canJump(const vector<int>& nums) {
4242
int reach = 1; // 最右能跳到哪里
43-
for (int i = 0; i < reach && reach < n; ++i)
44-
reach = max(reach, i + 1 + A[i]);
45-
return reach >= n;
43+
for (int i = 0; i < reach && reach < nums.size(); ++i)
44+
reach = max(reach, i + 1 + nums[i]);
45+
return reach >= nums.size();
4646
}
4747
};
4848
\end{Code}
@@ -54,13 +54,13 @@ \subsubsection{代码2}
5454
// 思路2,时间复杂度O(n),空间复杂度O(1)
5555
class Solution {
5656
public:
57-
bool canJump (int A[], int n) {
58-
if (n == 0) return true;
57+
bool canJump (const vector<int>& nums) {
58+
if (nums.empty()) return true;
5959
// 逆向下楼梯,最左能下降到第几层
60-
int left_most = n - 1;
60+
int left_most = nums.size() - 1;
6161

62-
for (int i = n - 2; i >= 0; --i)
63-
if (i + A[i] >= left_most)
62+
for (int i = nums.size() - 2; i >= 0; --i)
63+
if (i + nums[i] >= left_most)
6464
left_most = i;
6565

6666
return left_most == 0;
@@ -75,14 +75,14 @@ \subsubsection{代码3}
7575
// 思路三,动规,时间复杂度O(n),空间复杂度O(n)
7676
class Solution {
7777
public:
78-
bool canJump(int A[], int n) {
79-
vector<int> f(n, 0);
78+
bool canJump(const vector<int>& nums) {
79+
vector<int> f(nums.size(), 0);
8080
f[0] = 0;
81-
for (int i = 1; i < n; i++) {
82-
f[i] = max(f[i - 1], A[i - 1]) - 1;
81+
for (int i = 1; i < nums.size(); i++) {
82+
f[i] = max(f[i - 1], nums[i - 1]) - 1;
8383
if (f[i] < 0) return false;;
8484
}
85-
return f[n - 1] >= 0;
85+
return f[nums.size() - 1] >= 0;
8686
}
8787
};
8888
\end{Code}
@@ -121,18 +121,18 @@ \subsubsection{代码1}
121121
// 时间复杂度O(n),空间复杂度O(1)
122122
class Solution {
123123
public:
124-
int jump(int A[], int n) {
124+
int jump(const vector<int>& nums) {
125125
int step = 0; // 最小步数
126126
int left = 0;
127127
int right = 0; // [left, right]是当前能覆盖的区间
128-
if (n == 1) return 0;
128+
if (nums.size() == 1) return 0;
129129

130130
while (left <= right) { // 尝试从每一层跳最远
131131
++step;
132132
const int old_right = right;
133133
for (int i = left; i <= old_right; ++i) {
134-
int new_right = i + A[i];
135-
if (new_right >= n - 1) return step;
134+
int new_right = i + nums[i];
135+
if (new_right >= nums.size() - 1) return step;
136136

137137
if (new_right > right) right = new_right;
138138
}
@@ -150,18 +150,18 @@ \subsubsection{代码2}
150150
// 时间复杂度O(n),空间复杂度O(1)
151151
class Solution {
152152
public:
153-
int jump(int A[], int n) {
153+
int jump(const vector<int>& nums) {
154154
int result = 0;
155155
// the maximum distance that has been reached
156156
int last = 0;
157157
// the maximum distance that can be reached by using "ret+1" steps
158158
int cur = 0;
159-
for (int i = 0; i < n; ++i) {
159+
for (int i = 0; i < nums.size(); ++i) {
160160
if (i > last) {
161161
last = cur;
162162
++result;
163163
}
164-
cur = max(cur, i + A[i]);
164+
cur = max(cur, i + nums[i]);
165165
}
166166

167167
return result;

0 commit comments

Comments
 (0)