Commit 420e8c43 by anqiwa 😲

### 2021/9/7 update

parent d476c20e
 /* * @lc app=leetcode id=1186 lang=cpp * * [1186] Maximum Subarray Sum with One Deletion */ // @lc code=start class Solution { public: int maximumSum(vector &nums) { // state 0 -> not used the deletion // state 1-> used the deletion const int n = nums.size(); vector> dp(n, vector(2, 0)); int res = INT_MIN; // very tricky dp[0][0] = nums[0]; dp[0][1] = 0; if (n == 1) return dp[0][0]; for (int i = 1; i < n; ++i) { dp[i][0] = max(dp[i - 1][0] + nums[i], nums[i]); dp[i][1] = max(dp[i - 1][0], dp[i - 1][1] + nums[i]); res = max((dp[i][0], res), dp[i][1]); } res = max(max(dp[n - 1][0], res), dp[n - 1][1]); //cout << dp[n - 1][0]; return res; } }; // @lc code=end
 class Solution { public: int minCost(vector> &costs) { int red = costs[0][0], blue = costs[0][1], green = costs[0][2]; const int n = costs.size(); for (int i = 1; i < n; ++i) { int old_red = red; int old_blue = blue; int old_green = green; red = min(old_blue, old_green) + costs[i][0]; blue = min(old_green, old_red) + costs[i][1]; green = min(old_red, old_blue) + costs[i][2]; } return min(min(red, blue), green); } }; \ No newline at end of file
 class Solution { public: int findMaxConsecutiveOnes(vector &nums) { int s0 = 0, s1 = 0; int result = 0; for (int i = 0; i < nums.size(); i++) { if (nums[i] == 0) { s1 = s0 + 1; s0 = 0; } else { s0++; s1++; } //smart result = max(result, s0); result = max(result, s1); } return result; } }; \ No newline at end of file
 /* * @lc app=leetcode id=53 lang=cpp * * [53] Maximum Subarray */ // @lc code=start class Solution { public: int maxSubArray(vector &nums) { //基本的时间序列dp // kadane's algo int res = nums[0]; const int n = nums.size(); vector dp(n, INT_MIN); dp[0] = nums[0]; for (int i = 1; i < n; ++i) { dp[i] = max(dp[i - 1] + nums[i], nums[i]); res = max(res, dp[i]); } return res; } }; // @lc code=end //[-2, 1, -3, 4, -1, 2, 1, -5, 4]
 /* * @lc app=leetcode id=68 lang=cpp * * [68] Text Justification */ // @lc code=start class Solution { public: vector fullJustify(vector &words, int maxWidth) { vector res; const int n = words.size(); int idx = 0; // outer loop while (idx < n) { // total num of char so far // add in the first word int totalChars = words[idx].size(); int last = idx + 1; // where the last word is while (last < n) { if (totalChars + 1 + words[last].size() > maxWidth) break; totalChars += 1 + words[last].size(); last++; } int gaps = last - idx - 1; string s = ""; if (last == n || gaps == 0) { for (int i = idx; i < last; ++i) { s += words[i]; s.push_back(' '); } s.pop_back(); while (s.size() < maxWidth) { s.push_back(' '); } } else { int spaces = (maxWidth - totalChars) / gaps; int rest = (maxWidth - totalChars) % gaps; for (int i = idx; i < last - 1; i++) { s += words[i]; s.push_back(' '); for (int j = 0; j < spaces + (i - idx < rest ? 1 : 0); j++) { s.push_back(' '); } } s += words[last - 1]; } res.push_back(s); idx = last; } //while return res; } }; // @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