diff --git a/.clang-format b/.clang-format index abf7494..9607d5b 100644 --- a/.clang-format +++ b/.clang-format @@ -1,7 +1,7 @@ BasedOnStyle: Google DerivePointerAlignment: false # PointerAlignment: Right -# ColumnLimit: 120 +ColumnLimit: 120 # Default for clang-8, changed in later clangs. Set explicitly for forwards # compatibility for students with modern clangs diff --git a/daily/June8.cpp b/daily/June8.cpp new file mode 100644 index 0000000..7403036 --- /dev/null +++ b/daily/June8.cpp @@ -0,0 +1,69 @@ +#include +using namespace std; + +class Solution { + public: + /** + * @brief return the max operations needed to get same score II + * This time we have options, first two, last two or first and last + * So this is a DFS problem, use cache or dp + * + * @param nums + * @return int + */ + int maxOperations(vector& nums) { + int len = nums.size(); + vector> memo(len, vector(len)); + auto helper = [&](int i, int j, int target) -> int { + for (auto& row : memo) { + std::fill(row.begin(), row.end(), -1); + } + function dfs = [&](int i, int j) -> int { + if (i >= j) return 0; + int& res = memo[i][j]; // reference + if (res != -1) return res; // get this before + res = 0; + if (nums[i] + nums[i + 1] == target) res = max(res, dfs(i + 2, j) + 1); + if (nums[j] + nums[j - 1] == target) res = max(res, dfs(i, j - 2) + 1); + if (nums[i] + nums[j] == target) res = max(res, dfs(i + 1, j - 1) + 1); + return res; + }; + return dfs(i, j); + }; + int res1 = helper(2, len - 1, nums[0] + nums[1]); // first two nums + int res2 = helper(0, len - 3, nums[len - 1] + nums[len - 2]); // last two nums + int res3 = helper(1, len - 2, nums[0] + nums[len - 1]); // first and last + return max({res1, res2, res3}) + 1; // add first op + } + + int maxOperationsOp(vector& nums) { + int len = nums.size(); + vector> memo(len, vector(len)); + bool done = false; // marker + auto helper = [&](int i, int j, int target) -> int { + if (done) return 0; + for (auto& row : memo) { + std::fill(row.begin(), row.end(), -1); // no get before + } + function dfs = [&](int i, int j) -> int { + if (done) return 0; + if (i >= j) { + done = true; + return 0; + } + int& res = memo[i][j]; + if (res != -1) return res; // got before + res = 0; + if (nums[i] + nums[i + 1] == target) res = max(res, dfs(i + 2, j) + 1); + if (nums[j] + nums[j - 1] == target) res = max(res, dfs(i, j - 2) + 1); + if (nums[i] + nums[j] == target) res = max(res, dfs(i + 1, j - 1) + 1); + return res; + }; + return dfs(i, j); + }; + int res1 = helper(2, len - 1, nums[0] + nums[1]); // first two + int res2 = helper(1, len - 2, nums[0] + nums[len - 1]); // first and last + int res3 = helper(0, len - 3, nums[len - 1] + nums[len - 2]); // last two + return max({res1, res2, res3}) + 1; // all max + 1 op + } +}; \ No newline at end of file