From eaa232d74578c2ce3c09012bdcedc1cde331e205 Mon Sep 17 00:00:00 2001 From: begeekmyfriend Date: Tue, 19 Dec 2023 16:02:37 +0800 Subject: [PATCH] Improvement Signed-off-by: begeekmyfriend --- 0042_trapping_rain_water/trap_water.c | 22 ++++++++---------- 0042_trapping_rain_water/trap_water.cc | 31 +++++++++++--------------- 0045_jump_game_ii/jump_game.c | 23 ++++++++++--------- 0045_jump_game_ii/jump_game.cc | 21 ++++++++--------- 0198_house_robber/robber.c | 6 +++-- 0198_house_robber/robber.cc | 6 +++-- 6 files changed, 54 insertions(+), 55 deletions(-) diff --git a/0042_trapping_rain_water/trap_water.c b/0042_trapping_rain_water/trap_water.c index a304e24..d0abc5b 100644 --- a/0042_trapping_rain_water/trap_water.c +++ b/0042_trapping_rain_water/trap_water.c @@ -35,22 +35,18 @@ static int trap(int* height, int heightSize) int res = 0; int l = 0, lmax = 0; int r = heightSize - 1, rmax = 0; + while (l < r) { - if (height[l] < height[r]) { - /* Only lmax is needed for lmax < rmax here */ - if (height[l] > lmax) { - lmax = height[l]; - } else { - res += lmax - height[l]; - } + /* lmax is the highest in height[0...l] and + * rmax is the highest in height[r...size - 1] + */ + lmax = height[l] > lmax ? height[l] : lmax; + rmax = height[r] > rmax ? height[r] : rmax; + if (lmax < rmax) { + res += lmax - height[l]; l++; } else { - /* Only rmax is needed for rmax < lmax here */ - if (height[r] > rmax) { - rmax = height[r]; - } else { - res += rmax - height[r]; - } + res += rmax - height[r]; r--; } } diff --git a/0042_trapping_rain_water/trap_water.cc b/0042_trapping_rain_water/trap_water.cc index dd91e7e..5649a61 100644 --- a/0042_trapping_rain_water/trap_water.cc +++ b/0042_trapping_rain_water/trap_water.cc @@ -9,25 +9,20 @@ class Solution { * water level of the position would be determined by the opposite side. */ int res = 0; - int left = 0, left_max = 0; - int right = height.size() - 1, right_max = 0; - while (left < right) { - if (height[left] < height[right]) { - /* Only lmax is needed for lmax < rmax here */ - if (height[left] > left_max) { - left_max = height[left]; - } else { - res += left_max - height[left]; - } - left++; + int l = 0, l_max = 0; + int r = height.size() - 1, r_max = 0; + + while (l < r) { + // lmax is the highest in height[0...l] and + // rmax is the highest in height[r...size - 1] + l_max = max(height[l], l_max); + r_max = max(height[r], r_max); + if (l_max < r_max) { + res += l_max - height[l]; + l++; } else { - /* Only rmax is needed for rmax < lmax here */ - if (height[right] > right_max) { - right_max = height[right]; - } else { - res += right_max - height[right]; - } - right--; + res += r_max - height[r]; + r--; } } diff --git a/0045_jump_game_ii/jump_game.c b/0045_jump_game_ii/jump_game.c index d315766..9de0eda 100644 --- a/0045_jump_game_ii/jump_game.c +++ b/0045_jump_game_ii/jump_game.c @@ -1,6 +1,7 @@ #include #include + static inline int max(int a, int b) { return a > b ? a : b; @@ -8,19 +9,21 @@ static inline int max(int a, int b) static int jump(int* nums, int numsSize) { - int i, lo = 0, hi = 0; + int i, right = 0; int steps = 0; - while (hi < numsSize - 1) { - int right = 0; - for (i = lo; i <= hi; i++) { - /* Assume right > hi for the purpose of the problem */ - right = max(i + nums[i], right); + int fartest = 0; + /* 1. Exhaust all the right boundries in the location range of [i...right] + * 2. When the search ends up with i==right, update the right boundry as + * the fartest position. + * 3. When the search ends up with i==right, it records as one jump step */ + for (i = 0; i < numsSize; i++) { + fartest = max(i + nums[i], fartest); + if (i == right) { + right = fartest; + steps++; } - /* [lo, hi] is the next location range */ - lo = hi + 1; - hi = right; - steps++; } + return steps; } diff --git a/0045_jump_game_ii/jump_game.cc b/0045_jump_game_ii/jump_game.cc index 77e6f2f..e561405 100644 --- a/0045_jump_game_ii/jump_game.cc +++ b/0045_jump_game_ii/jump_game.cc @@ -6,17 +6,18 @@ class Solution { public: int jump(vector& nums) { int steps = 0; - int lo = 0, hi = 0; - while (hi < nums.size() - 1) { - int right = 0; - for (int i = lo; i <= hi; i++) { - // right > hi for nums[i] > 0 - right = max(i + nums[i], right); + int right = 0; + int farthest = 0; + // 1. Exhaust all the right boundries in the location range of [i...right] + // 2. When the search ends up with i==right, update the right boundry as + // the fartest position. + // 3. When the search ends up with i==right, it records as one jump step */ + for (int i = 0; i < nums.size() - 1; i++) { + fartest = max(i + nums[i], fartest); + for (i == right) { + right = fartest; + steps++; } - // [lo, hi] is the next location range - lo = hi + 1; - hi = right; - steps++; } return steps; diff --git a/0198_house_robber/robber.c b/0198_house_robber/robber.c index d76228b..82fa8ad 100644 --- a/0198_house_robber/robber.c +++ b/0198_house_robber/robber.c @@ -14,10 +14,12 @@ static int rob(int* nums, int numsSize) int untaken = 0; /* Record max profits of nums[0...i] respectively */ for (i = 0; i < numsSize; i++) { - int tmp_taken = taken; + int last_taken = taken; /* Taken or untaken nums[i] */ + /* last taken + nums[i] */ taken = untaken + nums[i]; - untaken = max(tmp_taken, untaken); + /* max(last untaken, last taken) */ + untaken = max(last_taken, untaken); } return max(taken, untaken); diff --git a/0198_house_robber/robber.cc b/0198_house_robber/robber.cc index 3d2ffb5..010c571 100644 --- a/0198_house_robber/robber.cc +++ b/0198_house_robber/robber.cc @@ -8,9 +8,11 @@ class Solution { int taken = 0; int untaken = 0; for (int i = 0; i < nums.size(); i++) { - int tmp_taken = taken; + int last_taken = taken; + /* last untaken + nums[i]*/ taken = untaken + nums[i]; - untaken = max(untaken, tmp_taken); + /* max(last untaken, last taken) */ + untaken = max(untaken, last_taken); } return max(taken, untaken); }