Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit eaa232d

Browse filesBrowse files
Improvement
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent c40b4b8 commit eaa232d
Copy full SHA for eaa232d

File tree

Expand file treeCollapse file tree

6 files changed

+54
-55
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+54
-55
lines changed

‎0042_trapping_rain_water/trap_water.c

Copy file name to clipboardExpand all lines: 0042_trapping_rain_water/trap_water.c
+9-13Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,18 @@ static int trap(int* height, int heightSize)
3535
int res = 0;
3636
int l = 0, lmax = 0;
3737
int r = heightSize - 1, rmax = 0;
38+
3839
while (l < r) {
39-
if (height[l] < height[r]) {
40-
/* Only lmax is needed for lmax < rmax here */
41-
if (height[l] > lmax) {
42-
lmax = height[l];
43-
} else {
44-
res += lmax - height[l];
45-
}
40+
/* lmax is the highest in height[0...l] and
41+
* rmax is the highest in height[r...size - 1]
42+
*/
43+
lmax = height[l] > lmax ? height[l] : lmax;
44+
rmax = height[r] > rmax ? height[r] : rmax;
45+
if (lmax < rmax) {
46+
res += lmax - height[l];
4647
l++;
4748
} else {
48-
/* Only rmax is needed for rmax < lmax here */
49-
if (height[r] > rmax) {
50-
rmax = height[r];
51-
} else {
52-
res += rmax - height[r];
53-
}
49+
res += rmax - height[r];
5450
r--;
5551
}
5652
}

‎0042_trapping_rain_water/trap_water.cc

Copy file name to clipboardExpand all lines: 0042_trapping_rain_water/trap_water.cc
+13-18Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,20 @@ class Solution {
99
* water level of the position would be determined by the opposite side.
1010
*/
1111
int res = 0;
12-
int left = 0, left_max = 0;
13-
int right = height.size() - 1, right_max = 0;
14-
while (left < right) {
15-
if (height[left] < height[right]) {
16-
/* Only lmax is needed for lmax < rmax here */
17-
if (height[left] > left_max) {
18-
left_max = height[left];
19-
} else {
20-
res += left_max - height[left];
21-
}
22-
left++;
12+
int l = 0, l_max = 0;
13+
int r = height.size() - 1, r_max = 0;
14+
15+
while (l < r) {
16+
// lmax is the highest in height[0...l] and
17+
// rmax is the highest in height[r...size - 1]
18+
l_max = max(height[l], l_max);
19+
r_max = max(height[r], r_max);
20+
if (l_max < r_max) {
21+
res += l_max - height[l];
22+
l++;
2323
} else {
24-
/* Only rmax is needed for rmax < lmax here */
25-
if (height[right] > right_max) {
26-
right_max = height[right];
27-
} else {
28-
res += right_max - height[right];
29-
}
30-
right--;
24+
res += r_max - height[r];
25+
r--;
3126
}
3227
}
3328

‎0045_jump_game_ii/jump_game.c

Copy file name to clipboardExpand all lines: 0045_jump_game_ii/jump_game.c
+13-10Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4+
45
static inline int max(int a, int b)
56
{
67
return a > b ? a : b;
78
}
89

910
static int jump(int* nums, int numsSize)
1011
{
11-
int i, lo = 0, hi = 0;
12+
int i, right = 0;
1213
int steps = 0;
13-
while (hi < numsSize - 1) {
14-
int right = 0;
15-
for (i = lo; i <= hi; i++) {
16-
/* Assume right > hi for the purpose of the problem */
17-
right = max(i + nums[i], right);
14+
int fartest = 0;
15+
/* 1. Exhaust all the right boundries in the location range of [i...right]
16+
* 2. When the search ends up with i==right, update the right boundry as
17+
* the fartest position.
18+
* 3. When the search ends up with i==right, it records as one jump step */
19+
for (i = 0; i < numsSize; i++) {
20+
fartest = max(i + nums[i], fartest);
21+
if (i == right) {
22+
right = fartest;
23+
steps++;
1824
}
19-
/* [lo, hi] is the next location range */
20-
lo = hi + 1;
21-
hi = right;
22-
steps++;
2325
}
26+
2427
return steps;
2528
}
2629

‎0045_jump_game_ii/jump_game.cc

Copy file name to clipboardExpand all lines: 0045_jump_game_ii/jump_game.cc
+11-10Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ class Solution {
66
public:
77
int jump(vector<int>& nums) {
88
int steps = 0;
9-
int lo = 0, hi = 0;
10-
while (hi < nums.size() - 1) {
11-
int right = 0;
12-
for (int i = lo; i <= hi; i++) {
13-
// right > hi for nums[i] > 0
14-
right = max(i + nums[i], right);
9+
int right = 0;
10+
int farthest = 0;
11+
// 1. Exhaust all the right boundries in the location range of [i...right]
12+
// 2. When the search ends up with i==right, update the right boundry as
13+
// the fartest position.
14+
// 3. When the search ends up with i==right, it records as one jump step */
15+
for (int i = 0; i < nums.size() - 1; i++) {
16+
fartest = max(i + nums[i], fartest);
17+
for (i == right) {
18+
right = fartest;
19+
steps++;
1520
}
16-
// [lo, hi] is the next location range
17-
lo = hi + 1;
18-
hi = right;
19-
steps++;
2021
}
2122

2223
return steps;

‎0198_house_robber/robber.c

Copy file name to clipboardExpand all lines: 0198_house_robber/robber.c
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ static int rob(int* nums, int numsSize)
1414
int untaken = 0;
1515
/* Record max profits of nums[0...i] respectively */
1616
for (i = 0; i < numsSize; i++) {
17-
int tmp_taken = taken;
17+
int last_taken = taken;
1818
/* Taken or untaken nums[i] */
19+
/* last taken + nums[i] */
1920
taken = untaken + nums[i];
20-
untaken = max(tmp_taken, untaken);
21+
/* max(last untaken, last taken) */
22+
untaken = max(last_taken, untaken);
2123
}
2224

2325
return max(taken, untaken);

‎0198_house_robber/robber.cc

Copy file name to clipboardExpand all lines: 0198_house_robber/robber.cc
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ class Solution {
88
int taken = 0;
99
int untaken = 0;
1010
for (int i = 0; i < nums.size(); i++) {
11-
int tmp_taken = taken;
11+
int last_taken = taken;
12+
/* last untaken + nums[i]*/
1213
taken = untaken + nums[i];
13-
untaken = max(untaken, tmp_taken);
14+
/* max(last untaken, last taken) */
15+
untaken = max(untaken, last_taken);
1416
}
1517
return max(taken, untaken);
1618
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.