Commit d476c20e by anqiwa 😲

### 9/23/2021

parent be5ecb7c
 /* * @lc app=leetcode id=123 lang=cpp * * [123] Best Time to Buy and Sell Stock III */ // @lc code=start class Solution { public: int maxProfit(vector &prices) { // 时间序列dp const int n = prices.size(); //0-> just buy the first stock //1-> just sell the first stock //2-> just buy the second stock //3-> just sell the second stock vector> dp(n, vector(4, 0)); for (int i = 1; i < n; i++) { dp[i][0] = max(dp[i - 1][0], -prices[i]); dp[i][1] = max(dp[i - 1][0] + prices[i], dp[i - 1][1]); dp[i][2] = max(dp[i - 1][1] - prices[i], dp[i - 1][2]); dp[i][3] = max(dp[i - 1][3], dp[i - 1][2] + prices[i]); } return *max_element(dp[n - 1].begin(), dp[n - 1].end()); } }; // @lc code=end
 /* * @lc app=leetcode id=1235 lang=cpp * * [1235] Maximum Profit in Job Scheduling */ // @lc code=start class Solution { public: int jobScheduling(vector &startTime, vector &endTime, vector &profit) { } }; // @lc code=end
 /* * @lc app=leetcode id=21 lang=cpp * * [21] Merge Two Sorted Lists */ // @lc code=start /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode dummy(0, nullptr); ListNode *dumPtr = &dummy; while (l1 && l2) { if (l1->val <= l2->val) { ListNode *tmp = l1->next; dumPtr->next = l1; dumPtr = dumPtr->next; l1 = tmp; } else { ListNode *tmp = l2->next; dumPtr->next = l2; dumPtr = dumPtr->next; l2 = tmp; } } if (l1) { dumPtr->next = l1; } if (l2) { dumPtr->next = l2; } return dummy.next; } }; // @lc code=end
 /* * @lc app=leetcode id=215 lang=cpp * * [215] Kth Largest Element in an Array */ // @lc code=start class Solution { public: int findKthLargest(vector &nums, int k) { //* try out the QuickSelect //* this implementation literally //* copy from the 477 lec with a slight modification //* this is very slow if (nums.size() == 1) return nums[0]; int pivot = nums[0]; // randomly select a num //cout << pivot< left; // # > pivot vector right; // # <= pivot // rank pivot for (int i = 1; i < nums.size(); ++i) { if (nums[i] > pivot) left.push_back(nums[i]); else right.push_back(nums[i]); } // for if (left.size() == k - 1) return pivot; else if (left.size() > k - 1) return findKthLargest(left, k); else return findKthLargest(right, k - left.size() - 1); } }; // @lc code=end
 /* * @lc app=leetcode id=215 lang=cpp * * [215] Kth Largest Element in an Array */ // @lc code=start class Solution { //* still quicksort //* but use 3 ptr method to improve the partition process //* this 3 ptr method is inspired from lc 075 public: int findKthLargest(vector &nums, int k) { return quickselect(nums, 0, nums.size() - 1, k); } int quickselect(vector &nums, int begin, int end, int k) { int pivot = nums[(begin + end) / 2]; int i = begin, j = end; int t = begin; while (t <= j) { if (nums[t] < pivot) { swap(nums[i], nums[t]); i++; t++; } else if (nums[t] > pivot) { swap(nums[j], nums[t]); j--; } else t++; } if (end - j >= k) return quickselect(nums, j + 1, end, k); else if (end - i + 1 >= k) return pivot; else return quickselect(nums, begin, i - 1, k - (end - i + 1)); } }; // @lc code=end
 /* * @lc app=leetcode id=215 lang=cpp * * [215] Kth Largest Element in an Array */ // @lc code=start class Solution { //* still quicksort //* but use 3 ptr method to improve the partition process //* this 3 ptr method is inspired from lc 075 public: int findKthLargest(vector &nums, int k) { return quickselect(nums, 0, nums.size() - 1, k); } int quickselect(vector &nums, int begin, int end, int k) { int pivot = nums[(begin + end) / 2]; int i = begin, j = end; int t = begin; while (t <= j) { if (nums[t] < pivot) { swap(nums[i], nums[t]); i++; t++; } else if (nums[t] > pivot) { swap(nums[j], nums[t]); j--; } else t++; } if (end - j >= k) return quickselect(nums, j + 1, end, k); else if (end - i + 1 >= k) return pivot; else return quickselect(nums, begin, i - 1, k - (end - i + 1)); } }; // @lc code=end
 /* * @lc app=leetcode id=242 lang=cpp * * [242] Valid Anagram */ // @lc code=start class Solution { public: bool isAnagram(string s, string t) { if (s.size() != t.size()) return false; const int n = s.size(); vector map(26, 0); for (int i = 0; i < n; ++i) { map[s[i] - 'a']++; map[t[i] - 'a']--; } for (int j = 0; j < 26; ++j) { if (map[j] != 0) return false; } return true; } }; // @lc code=end
 /** * 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 { int res = 0; double minDiff = std::numeric_limits::max(); public: int closestValue(TreeNode *root, double target) { this->res = root->val; minDiff = abs((double)root->val - target); preOrder(root, target); return res; } void preOrder(TreeNode *root, double target) { if (!root) return; double currDiff = abs((double)root->val - target); if (currDiff < minDiff) { minDiff = currDiff; res = root->val; } preOrder(root->left, target); preOrder(root->right, target); } };
 class Solution { public: int numWays(int n, int k) { // two states: // same: nums[i-1] == nums[i] // diff : nums[i-1] != nums[i] if (n == 0) return 0; if (n == 1) return k; int same = k; int diff = k * (k - 1); for (int i = 2; i < n; ++i) { int old_same = same; int old_diff = diff; same = old_diff; diff = old_same * (k - 1) + old_diff * (k - 1); } return same + diff; } }; \ No newline at end of file
 /* * @lc app=leetcode id=3 lang=cpp * * [3] Longest Substring Without Repeating Characters */ // @lc code=start class Solution { public: int lengthOfLongestSubstring(string s) { } }; // @lc code=end
 ... ... @@ -5,11 +5,23 @@ */ // @lc code=start class Solution { class Solution { public: int lengthOfLongestSubstring(string s) { int lengthOfLongestSubstring(string s) { //* idea: 移动右边指针，并且在 o(1) 时间快速判断出左边指针的位置 unordered_map Map; // record the pos of each char's last appear int i = 0; int result = 0; for (int j = 0; j < s.size(); j++) { if (Map.find(s[j]) != Map.end()) i = max(i, Map[s[j]] + 1); Map[s[j]] = j; result = max(result, j - i + 1); } return result; } }; // @lc code=end
 /* * @lc app=leetcode id=309 lang=cpp * * [309] Best Time to Buy and Sell Stock with Cooldown */ // @lc code=start class Solution { public: int maxProfit(vector &prices) { // 时间序列dp // state 0: holding a stock // state 1: just sold // state 2: ready to buy // 0 -> 1, 0 -> 0 // 1 -> 2 // 2 -> 0, 2 -> 2 // STE //* dp[i][0] = max {dp[i-1][0], dp[i-1][2]-price[i]} //* dp[i][1] = max{dp[i-1][0] + price[i]} //* dp[i][2] = max{dp[i-1][1], dp[i-1][2]} //* HARDNESS: what initial state we should set int hold = INT_MIN; int sold = 0; int ready = 0; const int n = prices.size(); for (int i = 0; i < n; ++i) { // store the yesterday states int past_hold = hold; int past_sold = sold; int past_ready = ready; hold = max(past_hold, past_ready - prices[i]); sold = past_hold + prices[i]; ready = max(past_sold, past_ready); } //for return max(sold, ready); } }; // @lc code=end
 /* * @lc app=leetcode id=376 lang=cpp * * [376] Wiggle Subsequence */ // @lc code=start class Solution { public: int wiggleMaxLength(vector &nums) { //state 0: with i end seq is raising //state 1: with i end seq is falling int raise = 1; int fall = 1; const int n = nums.size(); for (int i = 1; i < n; ++i) { int past_raise = raise; int past_fall = fall; if (nums[i - 1] < nums[i]) { raise = past_fall + 1; fall = past_fall; } else if (nums[i - 1] > nums[i]) { fall = past_raise + 1; raise = past_raise; } } return max(fall, raise); } }; // @lc code=end
 /* * @lc app=leetcode id=75 lang=cpp * * [75] Sort Colors */ // @lc code=start class Solution { public: void sortColors(vector &nums) { int i = 0; int t = 0; int j = nums.size() - 1; while (j >= t) { if (nums[t] == 0) { swap(nums[i], nums[t]); ++t; ++i; } else if (nums[t] == 2) { swap(nums[t], nums[j]); --j; } else ++t; } //while } }; // @lc code=end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!