|
7 | 7 | 4. [Best time to buy and sell stocks II] Medium |
8 | 8 | 5. [Best Time to Buy and Sell Stock III] Medium |
9 | 9 | 6. [Best time to buy and sell stock with transcation fee](#Best-time-to-buy-and-sell-stock-with-transcation-fee) Medium |
10 | | -7. [Diagnose Tranverse](#Diagnose-Tranverse) Medium |
11 | | -8. [Insert Delete GetRandom O(1)](#Insert-Delete-GetRandom-O(1)) Medium |
12 | | -9. [Product of Array Except Self](#product-of-array-except-self) Medium |
13 | | -10. [Kth Smallest Element in a Sorted Matrix](#Kth-Smallest-Element-in-a-Sorted-Matrix) Medium |
14 | | -11. [Rotate Image](#Rotate-Image) Medium |
15 | | -12. [Maximum Swap](#Maximum-Swap) Medium |
16 | | -13. [Intersection of Two Arrays](#Intersection-of-Two-Arrays) Easy |
17 | | -14. [Intersection of Two Arrays II](#Intersection-of-Two-Arrays-II) Easy |
18 | | -15. [Sparse Matrix Multiplication](#Sparse-Matrix-Multiplication) Medium |
19 | | -16. [Rectangle Overlap](#Rectangle-Overlap) Easy |
20 | | -17. [Minimum Window Substring](#Minimum-Window-Substring) Hard |
21 | | -18. [Valid Number](#Valid-Number) Hard |
22 | | -19. [Candy](#Candy) Hard |
23 | | -20. [Subarray Sum Equals K](#Subarray-Sum-Equals-K) Medium |
| 10 | +7. [Insert Delete GetRandom O(1)](#Insert-Delete-GetRandom-O(1)) Medium |
| 11 | +8. [Product of Array Except Self](#product-of-array-except-self) Medium |
| 12 | +9. [Maximum Swap](#Maximum-Swap) Medium |
| 13 | +10. [Intersection of Two Arrays](#Intersection-of-Two-Arrays) Easy |
| 14 | +11. [Intersection of Two Arrays II](#Intersection-of-Two-Arrays-II) Easy |
| 15 | +12. [Sparse Matrix Multiplication](#Sparse-Matrix-Multiplication) Medium |
| 16 | +13. [Rectangle Overlap](#Rectangle-Overlap) Easy |
| 17 | +14. [Minimum Window Substring](#Minimum-Window-Substring) Hard |
| 18 | +15. [Valid Number](#Valid-Number) Hard |
| 19 | +16. [Candy](#Candy) Hard |
| 20 | +17. [Subarray Sum Equals K](#Subarray-Sum-Equals-K) Medium |
| 21 | +18. [Find the Kth largest item](#Find-the-Kth-largest-item) Medium |
| 22 | +19. [Count Duplicates](#Count-Duplicates) Medium |
| 23 | +20. [3 Sum](#3-Ssum) |
| 24 | + |
| 25 | +## Matrix |
| 26 | +1. [Diagnose Tranverse](#Diagnose-Tranverse) Medium |
| 27 | +2. [Kth Smallest Element in a Sorted Matrix](#Kth-Smallest-Element-in-a-Sorted-Matrix) Medium |
| 28 | +3. [Rotate Image](#Rotate-Image) Medium |
24 | 29 |
|
25 | 30 | ## Binary Search |
26 | 31 | 1. [Search a 2D Matrix](#Search-a-2D-Matrix) Easy |
|
43 | 48 | 10. [Rotate List](#Rotate-List) Medium |
44 | 49 | 11. [Plus One Linked List](#Plus-One-Linked-List) Medium |
45 | 50 | 12. [Intersection of Two Linked Lists](#Intersection-of-Two-Linked-Lists) Medium |
| 51 | +13. [Remove Duplicates from Sorted List II](#Remove-Duplicates-from-Sorted-List-II) Medium |
| 52 | +14. [Remove Nth Node From End of List](#Remove-Nth node-from-the-end-of-list) Medium |
46 | 53 |
|
47 | 54 | ## String |
48 | 55 | 1. [Valid Anagram](#Valid-Anagram) Easy |
@@ -2855,4 +2862,180 @@ string minWindow(string s, string t) { |
2855 | 2862 | } |
2856 | 2863 | return result; |
2857 | 2864 | } |
| 2865 | +``` |
| 2866 | + |
| 2867 | +### Find the Kth largest item |
| 2868 | +***Big O:*** O(n) speed, O(1) space |
| 2869 | +``` |
| 2870 | +Tips: |
| 2871 | +
|
| 2872 | +Quick Select |
| 2873 | +``` |
| 2874 | +```c++ |
| 2875 | +class Solution { |
| 2876 | +public: |
| 2877 | + int findKthLargest(vector<int>& nums, int k) { |
| 2878 | + //partition rule: >=pivot pivot <=pivot |
| 2879 | + int left=0,right=nums.size()-1,idx=0; |
| 2880 | + while(1){ |
| 2881 | + idx = partition(nums,left,right); |
| 2882 | + if(idx==k-1) break; |
| 2883 | + else if(idx < k-1) left=idx+1; |
| 2884 | + else right= idx-1; |
| 2885 | + } |
| 2886 | + return nums[idx]; |
| 2887 | + } |
| 2888 | + int partition(vector<int>& nums,int left,int right){//hoare partition |
| 2889 | + int pivot = nums[left], l=left+1, r = right; |
| 2890 | + while(l<=r){ |
| 2891 | + if(nums[l]<pivot && nums[r]>pivot) swap(nums[l++],nums[r--]); |
| 2892 | + if(nums[l]>=pivot) ++l; |
| 2893 | + if(nums[r]<=pivot) --r; |
| 2894 | + } |
| 2895 | + swap(nums[left], nums[r]); |
| 2896 | + return r; |
| 2897 | + } |
| 2898 | +}; |
| 2899 | +``` |
| 2900 | +
|
| 2901 | +### Count Duplicates |
| 2902 | +***Big O:*** O(n) speed, O(n) space |
| 2903 | +``` |
| 2904 | +Tips: |
| 2905 | + |
| 2906 | +Hashmap |
| 2907 | +``` |
| 2908 | +```c++ |
| 2909 | +class Solution { |
| 2910 | +public: |
| 2911 | + vector<int> countduplicates(vector<int> &nums) { |
| 2912 | + // write your code here |
| 2913 | + unordered_map<int, bool> dup; |
| 2914 | + vector<int> ans; |
| 2915 | +
|
| 2916 | + for (auto n : nums) { |
| 2917 | + if ((dup.find(n) != dup.end()) && !dup[n]) { |
| 2918 | + ans.push_back(n); |
| 2919 | + dup[n] = true; |
| 2920 | + } |
| 2921 | + else if (dup.find(n) == dup.end()) |
| 2922 | + dup[n] = false; |
| 2923 | + } |
| 2924 | +
|
| 2925 | + return ans; |
| 2926 | + } |
| 2927 | +}; |
| 2928 | +``` |
| 2929 | + |
| 2930 | +### 3 Sum |
| 2931 | +***Big O:*** O(n^2) speed, O(1) space |
| 2932 | +``` |
| 2933 | +Tips: |
| 2934 | +
|
| 2935 | +Sort + 2 Sum II (two pointer). |
| 2936 | +
|
| 2937 | +Be careful with duplicate items. |
| 2938 | +``` |
| 2939 | +```c++ |
| 2940 | +class Solution { |
| 2941 | +public: |
| 2942 | + vector<vector<int>> threeSum(vector<int>& nums) { |
| 2943 | + sort(begin(nums), end(nums)); |
| 2944 | + vector<vector<int>> res; |
| 2945 | + for (int i = 0; i < nums.size() && nums[i] <= 0; ++i) |
| 2946 | + if (i == 0 || nums[i - 1] != nums[i]) { |
| 2947 | + twoSumII(nums, i, res); |
| 2948 | + } |
| 2949 | + return res; |
| 2950 | + } |
| 2951 | + void twoSumII(vector<int>& nums, int i, vector<vector<int>> &res) { |
| 2952 | + int lo = i + 1, hi = nums.size() - 1; |
| 2953 | + while (lo < hi) { |
| 2954 | + int sum = nums[i] + nums[lo] + nums[hi]; |
| 2955 | + if (sum < 0) { |
| 2956 | + ++lo; |
| 2957 | + } else if (sum > 0) { |
| 2958 | + --hi; |
| 2959 | + } else { |
| 2960 | + res.push_back({ nums[i], nums[lo++], nums[hi--] }); |
| 2961 | + while (lo < hi && nums[lo] == nums[lo - 1]) |
| 2962 | + ++lo; |
| 2963 | + } |
| 2964 | + } |
| 2965 | + } |
| 2966 | +}; |
| 2967 | +``` |
| 2968 | +
|
| 2969 | +### Remove Duplicates from Sorted List II |
| 2970 | +***Big O:*** O(n) speed, O(1) space |
| 2971 | +``` |
| 2972 | +Tips: |
| 2973 | + |
| 2974 | +Two pointers pre and cur. |
| 2975 | +``` |
| 2976 | +```c++ |
| 2977 | +class Solution { |
| 2978 | +public: |
| 2979 | + ListNode* deleteDuplicates(ListNode* head) { |
| 2980 | + ListNode sentinal, *pre, *cur, *nxt; |
| 2981 | + |
| 2982 | + sentinal.next = head; |
| 2983 | + sentinal.val = INT_MIN; |
| 2984 | + pre = &sentinal; |
| 2985 | + cur = pre; |
| 2986 | + |
| 2987 | + while (cur && cur->next) { |
| 2988 | + while (cur->next && cur->val == cur->next->val) { |
| 2989 | + cur = cur->next; |
| 2990 | + } |
| 2991 | + cur = cur->next; |
| 2992 | + pre->next = cur; |
| 2993 | + |
| 2994 | + if (cur && cur->next && cur->val != cur->next->val) |
| 2995 | + pre = pre->next; |
| 2996 | + } |
| 2997 | + |
| 2998 | + return sentinal.next; |
| 2999 | + } |
| 3000 | +}; |
| 3001 | +``` |
| 3002 | + |
| 3003 | +### Remove Nth node from the end of list |
| 3004 | +***Big O:*** O(L) speed, O(1) space |
| 3005 | +``` |
| 3006 | +Tips: |
| 3007 | +
|
| 3008 | +One pass algorithm: |
| 3009 | +
|
| 3010 | +The first pointer advances the list by n+1n+1 steps from the beginning, while the second pointer starts from the beginning of the list. Now, both pointers are exactly separated by nn nodes apart. We maintain this constant gap by advancing both pointers together until the first pointer arrives past the last node. The second pointer will be pointing at the nnth node counting from the last. We relink the next pointer of the node referenced by the second pointer to point to the node's next next node. |
| 3011 | +``` |
| 3012 | +```c++ |
| 3013 | +class Solution { |
| 3014 | +public: |
| 3015 | + ListNode* removeNthFromEnd(ListNode* head, int n) { |
| 3016 | + if (head == nullptr || (head->next == nullptr && n > 0)) |
| 3017 | + return nullptr; |
| 3018 | + |
| 3019 | + ListNode sentinal, *first, *second; |
| 3020 | + |
| 3021 | + sentinal.next = head; |
| 3022 | + first = &sentinal; |
| 3023 | + second = &sentinal; |
| 3024 | + |
| 3025 | + while(n--) { |
| 3026 | + if (second == nullptr) |
| 3027 | + return nullptr; |
| 3028 | + second = second->next; |
| 3029 | + } |
| 3030 | + |
| 3031 | + while(second->next) { |
| 3032 | + first = first->next; |
| 3033 | + second = second->next; |
| 3034 | + } |
| 3035 | + |
| 3036 | + first->next = first->next->next; |
| 3037 | + |
| 3038 | + return sentinal.next; |
| 3039 | + } |
| 3040 | +}; |
2858 | 3041 | ``` |
0 commit comments