File tree Expand file tree Collapse file tree 6 files changed +54
-55
lines changed Expand file tree Collapse file tree 6 files changed +54
-55
lines changed Original file line number Diff line number Diff line change @@ -35,22 +35,18 @@ static int trap(int* height, int heightSize)
35
35
int res = 0 ;
36
36
int l = 0 , lmax = 0 ;
37
37
int r = heightSize - 1 , rmax = 0 ;
38
+
38
39
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 ];
46
47
l ++ ;
47
48
} 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 ];
54
50
r -- ;
55
51
}
56
52
}
Original file line number Diff line number Diff line change @@ -9,25 +9,20 @@ class Solution {
9
9
* water level of the position would be determined by the opposite side.
10
10
*/
11
11
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 ++;
23
23
} 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--;
31
26
}
32
27
}
33
28
Original file line number Diff line number Diff line change 1
1
#include <stdio.h>
2
2
#include <stdlib.h>
3
3
4
+
4
5
static inline int max (int a , int b )
5
6
{
6
7
return a > b ? a : b ;
7
8
}
8
9
9
10
static int jump (int * nums , int numsSize )
10
11
{
11
- int i , lo = 0 , hi = 0 ;
12
+ int i , right = 0 ;
12
13
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 ++ ;
18
24
}
19
- /* [lo, hi] is the next location range */
20
- lo = hi + 1 ;
21
- hi = right ;
22
- steps ++ ;
23
25
}
26
+
24
27
return steps ;
25
28
}
26
29
Original file line number Diff line number Diff line change @@ -6,17 +6,18 @@ class Solution {
6
6
public:
7
7
int jump (vector<int >& nums) {
8
8
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++;
15
20
}
16
- // [lo, hi] is the next location range
17
- lo = hi + 1 ;
18
- hi = right;
19
- steps++;
20
21
}
21
22
22
23
return steps;
Original file line number Diff line number Diff line change @@ -14,10 +14,12 @@ static int rob(int* nums, int numsSize)
14
14
int untaken = 0 ;
15
15
/* Record max profits of nums[0...i] respectively */
16
16
for (i = 0 ; i < numsSize ; i ++ ) {
17
- int tmp_taken = taken ;
17
+ int last_taken = taken ;
18
18
/* Taken or untaken nums[i] */
19
+ /* last taken + nums[i] */
19
20
taken = untaken + nums [i ];
20
- untaken = max (tmp_taken , untaken );
21
+ /* max(last untaken, last taken) */
22
+ untaken = max (last_taken , untaken );
21
23
}
22
24
23
25
return max (taken , untaken );
Original file line number Diff line number Diff line change @@ -8,9 +8,11 @@ class Solution {
8
8
int taken = 0 ;
9
9
int untaken = 0 ;
10
10
for (int i = 0 ; i < nums.size (); i++) {
11
- int tmp_taken = taken;
11
+ int last_taken = taken;
12
+ /* last untaken + nums[i]*/
12
13
taken = untaken + nums[i];
13
- untaken = max (untaken, tmp_taken);
14
+ /* max(last untaken, last taken) */
15
+ untaken = max (untaken, last_taken);
14
16
}
15
17
return max (taken, untaken);
16
18
}
You can’t perform that action at this time.
0 commit comments