Skip to content

[pull] master from begeekmyfriend:master #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions 0042_trapping_rain_water/trap_water.c
Original file line number Diff line number Diff line change
Expand Up @@ -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--;
}
}
Expand Down
31 changes: 13 additions & 18 deletions 0042_trapping_rain_water/trap_water.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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--;
}
}

Expand Down
23 changes: 13 additions & 10 deletions 0045_jump_game_ii/jump_game.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
#include <stdio.h>
#include <stdlib.h>


static inline int max(int a, int b)
{
return a > b ? a : 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;
}

Expand Down
21 changes: 11 additions & 10 deletions 0045_jump_game_ii/jump_game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ class Solution {
public:
int jump(vector<int>& 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;
Expand Down
6 changes: 4 additions & 2 deletions 0198_house_robber/robber.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions 0198_house_robber/robber.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down