Skip to content

Commit

Permalink
update: May 6 and May 10
Browse files Browse the repository at this point in the history
  • Loading branch information
aucker committed May 10, 2024
1 parent 4fc3d17 commit 92e4fb0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
46 changes: 46 additions & 0 deletions daily/May10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
/**
* @brief Use the 2-loop
* Time: O(N^2)
*
* @param batteryPercentages
* @return int
*/
int countTestedDevices(vector<int>& batteryPercentages) {
int res = 0;
int i = 0;
int len = batteryPercentages.size();
while (i < len) {
if (batteryPercentages[i] > 0) {
res++;
for (int j = i; j < len; j++) {
batteryPercentages[j] = max(0, batteryPercentages[j] - 1);
}
}

i++;
}

return res;
}

/**
* @brief This is optimized version
* Time: O(N)
*
* @param batteryPercentages
* @return int
*/
int countTestedDevicesOP(vector<int>& batteryPercentages) {
int res = 0;
for (int x : batteryPercentages) {
res += res < x;
}

return res;
}
};
26 changes: 26 additions & 0 deletions daily/May6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
int cherryPickup(vector<vector<int>>& grid) {
int n = grid.size();
vector<vector<int>> f(n + 1, vector<int>(n + 1, INT_MIN));
f[1][1] = grid[0][0];
for (int t = 1; t < n * 2 - 1; t++) {
for (int j = min(t, n - 1); j >= max(t - n + 1, 0); j--) {
for (int k = min(t, n - 1); k >= j; k--) {
if (grid[t - j][j] < 0 || grid[t - k][k] < 0) {
f[j + 1][k + 1] = INT_MIN;
} else {
f[j + 1][k + 1] =
max({f[j + 1][k + 1], f[j + 1][k], f[j][k + 1], f[j][k]}) +
grid[t - j][j] + (k != j ? grid[t - k][k] : 0);
}
}
}
}

return max(f[n][n], 0);
}
};

0 comments on commit 92e4fb0

Please sign in to comment.