Commit de8a3287 by anqiwa 😲

### 9/1/21 update

parent d2dfd695
 /* * @lc app=leetcode id=198 lang=cpp * * [198] House Robber */ // @lc code=start class Solution { public: int rob(vector &nums) { //时间序列dp // dp[i][j] <= 第i轮 第j种状态 // 0: the max money without rob this house // 1: the max money rob this house vector> dp(nums.size(), vector(2, 0)); dp[0][1] = nums[0]; const int n = nums.size(); for (int i = 1; i < nums.size(); ++i) { dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]); dp[i][1] = dp[i - 1][0] + nums[i]; } //for return max(dp[n - 1][0], dp[n - 1][1]); } }; // @lc code=end
 /* * @lc app=leetcode id=200 lang=cpp * * [200] Number of Islands */ // @lc code=start class Solution { vector> dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; public: int numIslands(vector> &grid) { // either bfs ad dfs works // this is the bfs solution const int m = grid.size(); if (m == 0) return 0; const int n = grid[0].size(); int count = 0; // find the next 1 for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == '1') { count += 1; //do bfs here queue> q; //{pos} q.push({i, j}); grid[i][j] = '#'; while (!q.empty()) { auto [curr_x, curr_y] = q.front(); q.pop(); for (auto &dir : dirs) { int n_x = curr_x + dir.first; int n_y = curr_y + dir.second; if (n_x < 0 || n_y < 0 || n_x >= m || n_y >= n) continue; if (grid[n_x][n_y] != '1') continue; q.push({n_x, n_y}); grid[n_x][n_y] = '#'; } //for } //while } } //for } //for return count; } }; // @lc code=end
 /* * @lc app=leetcode id=213 lang=cpp * * [213] House Robber II */ // @lc code=start class Solution { public: int rob(vector &nums) { int n = nums.size(); vector> dp1(); vector> dp2(); } }; // @lc code=end
 /* * @lc app=leetcode id=282 lang=cpp * * [282] Expression Add Operators */ // @lc code=start class Solution { vector res; public: vector addOperators(string num, int target) { // 这题很多细节 string currExp = ""; long long currRes = 0; int curr = 0; dfs(num, target, curr, currRes, currExp, currRes); return res; } void dfs(string &num, int target, int curr, long long currRes, string currExp, long long lastTerm) { if (curr == num.size()) { if (currRes == target) res.push_back(currExp); return; } for (int i = curr; i < num.size(); ++i) { string currNumS = num.substr(curr, i - curr + 1); if (currNumS.size() > 1 && currNumS[0] == '0') continue; long long currNum = stoll(currNumS); if (curr == 0) dfs(num, target, curr + currNumS.size(), currRes + currNum, currExp + currNumS, currNum); else { dfs(num, target, curr + currNumS.size(), currRes + currNum, currExp + "+" + currNumS, currNum); dfs(num, target, curr + currNumS.size(), currRes - currNum, currExp + "-" + currNumS, -currNum); //notice, turn to minus dfs(num, target, curr + currNumS.size(), currRes - lastTerm + lastTerm * currNum, currExp + "*" + currNumS, lastTerm * currNum); } } } //dfs() }; // @lc code=end //123 + 4 * dfs(...) // \ No newline at end of file
 /* * @lc app=leetcode id=301 lang=cpp * * [301] Remove Invalid Parentheses */ // @lc code=start class Solution { vector rets; int retLen; public: vector removeInvalidParentheses(string s) { string curStr = ""; retLen = -1; dfs(curStr, s, 0, 0); return rets; } void dfs(string curStr, string &s, int i, int count) { if (count < 0) return; if (i == s.size()) { if (count != 0) return; if (curStr.size() == retLen) rets.push_back(curStr); else if ((int)curStr.size() > retLen) { rets.clear(); rets.push_back(curStr); retLen = curStr.size(); } return; } if (s[i] != '(' && s[i] != ')') { dfs(curStr + s.substr(i, 1), s, i + 1, count); return; } dfs(curStr + s.substr(i, 1), s, i + 1, count + (s[i] == '(' ? 1 : -1)); if (curStr.size() == 0 || curStr.back() != s[i]) dfs(curStr, s, i + 1, count); } }; // @lc code=end
 /* * @lc app=leetcode id=312 lang=cpp * * [312] Burst Balloons */ // @lc code=start class Solution { public: int maxCoins(vector &nums) { int n = nums.size(); nums.insert(nums.begin(), 1); nums.push_back(1); auto dp = vector>(n + 2, vector(n + 2, 0)); for (int len = 1; len <= n; len++) for (int i = 1; i + len - 1 <= n; i++) { int j = i + len - 1; for (int k = i; k <= j; k++) dp[i][j] = max( dp[i][j], dp[i][k - 1] + dp[k + 1][j] + nums[i - 1] * nums[k] * nums[j + 1]); } return dp[1][n]; } }; // @lc code=end // [1 x i xxx j] \ No newline at end of file
 /* * @lc app=leetcode id=473 lang=cpp * * [473] Matchsticks to Square */ // @lc code=start class Solution { int total = 0; public: bool makesquare(vector &matchsticks) { // more general: partition in k equ sum subsets // we add stick one by one group by one group total = accumulate(matchsticks.begin(), matchsticks.end(), 0); if (total % 4 != 0) return false; sort(matchsticks.begin(), matchsticks.end()); reverse(matchsticks.begin(), matchsticks.end()); // greedy vector visited(15, false); // at max 15 sticks return dfs(matchsticks, visited, 0, 0, 0); } bool dfs(vector &matchsticks, vector &visited, int curr, int currGrp, int currGrpSum) { if (currGrp == 4) return true; if (currGrpSum > total / 4) return false; if (currGrpSum == total / 4) return dfs(matchsticks, visited, 0, currGrp + 1, 0); for (int i = curr; i < matchsticks.size(); ++i) { if (visited[i]) continue; visited[i] = true; if (dfs(matchsticks, visited, i + 1, currGrp, currGrpSum + matchsticks[i])) return true; visited[i] = false; } //for return false; } }; // @lc code=end
 /* * @lc app=leetcode id=491 lang=cpp * * [491] Increasing Subsequences */ // @lc code=start class Solution { public: vector> findSubsequences(vector &nums) { } }; // @lc code=end
 /* * @lc app=leetcode id=698 lang=cpp * * [698] Partition to K Equal Sum Subsets */ // @lc code=start class Solution { int total = 0; int k = 0; public: bool canPartitionKSubsets(vector &nums, int k) { total = accumulate(nums.begin(), nums.end(), 0); this->k = k; if (total % k != 0) return false; vector visited(16, false); int curr = 0; int currGrp = 0; int currGrpSum = 0; return dfs(nums, visited, curr, currGrp, currGrpSum); } bool dfs(vector &nums, vector &visited, int curr, int currGrp, int currGrpSum) { // pruning if (currGrp == k) return true; if (currGrpSum > total / k) return false; if (currGrpSum == total / k) return dfs(nums, visited, 0, currGrp + 1, 0); for (int i = curr; i < nums.size(); ++i) { if (visited[i]) continue; visited[i] = true; if (dfs(nums, visited, i + 1, currGrp, currGrpSum + nums[i])) return true; visited[i] = false; } //for return false; } }; // @lc code=end
 /* * @lc app=leetcode id=921 lang=cpp * * [921] Minimum Add to Make Parentheses Valid */ // @lc code=start class Solution { public: int minAddToMakeValid(string s) { int count = 0; int res = 0; for (char ch : s) { if (ch == '(') count++; else if (ch == ')') count--; if (count < 0) { count++; res++; } // if } //for return count + res; } }; // @lc code=end
 /* * @lc app=leetcode id=958 lang=cpp * * [958] Check Completeness of a Binary Tree */ // @lc code=start /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: bool isCompleteTree(TreeNode *root) { //bfs // idea: from the right to the left, if we meet one null, we shoud not see any other child queue q; q.push(root); bool end = false; while (!q.empty()) { int len = q.size(); for (int i = 0; i < len; ++i) { TreeNode *curr = q.front(); q.pop(); if (curr->left) { if (end) return false; q.push(curr->left); } else end = true; if (curr->right) { if (end) return false; q.push(curr->right); } else end = true; } //for } //while return true; } }; // @lc code=end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment