From 92e4fb03aef4c54dfb6bededf937c4c62ebb3699 Mon Sep 17 00:00:00 2001 From: aucker Date: Fri, 10 May 2024 14:04:37 +0800 Subject: [PATCH] update: May 6 and May 10 --- daily/May10.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ daily/May6.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 daily/May10.cpp create mode 100644 daily/May6.cpp diff --git a/daily/May10.cpp b/daily/May10.cpp new file mode 100644 index 0000000..cda2acc --- /dev/null +++ b/daily/May10.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; + +class Solution { + public: + /** + * @brief Use the 2-loop + * Time: O(N^2) + * + * @param batteryPercentages + * @return int + */ + int countTestedDevices(vector& 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& batteryPercentages) { + int res = 0; + for (int x : batteryPercentages) { + res += res < x; + } + + return res; + } +}; \ No newline at end of file diff --git a/daily/May6.cpp b/daily/May6.cpp new file mode 100644 index 0000000..8a3cb39 --- /dev/null +++ b/daily/May6.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +class Solution { + public: + int cherryPickup(vector>& grid) { + int n = grid.size(); + vector> f(n + 1, vector(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); + } +}; \ No newline at end of file