|
61 | 61 | 12. Maximum Number of Occurrences of a Substring v |
62 | 62 | 13. Maximum Subarray v |
63 | 63 | 14. Move Zeroes v |
| 64 | +15. Find the Duplicate Number v |
| 65 | +16. Container With Most Water v |
| 66 | +17. Verifying an Alien Dictionary v |
64 | 67 |
|
65 | 68 | ***Math:*** |
66 | 69 | 1. Add Binary v |
|
72 | 75 |
|
73 | 76 | ***Matirx:*** |
74 | 77 | 1. Give the center of a matrix and then draw circle |
| 78 | +2. Rotate a matrix by 90 degree v |
| 79 | +3. Sparse Matrix Multiplication |
| 80 | +4. Search 2D Matrix |
75 | 81 |
|
76 | 82 | ***Data Structure:*** |
77 | 83 | 1. Insert Delete GetRandom O(1) v |
78 | | -2. LRU |
| 84 | +2. LRU Cache |
| 85 | +3. Design Add and Search Words Data Structure |
| 86 | + |
| 87 | +***String:*** |
| 88 | +1. Valid Palindrome II v |
79 | 89 |
|
80 | 90 | ### Impplementations |
| 91 | +Valid Palindrome II |
| 92 | +```c++ |
| 93 | +class Solution { |
| 94 | + |
| 95 | +public: |
| 96 | + // Checks if string is a palindrome |
| 97 | + bool isPalin(string &s, int start, int end) { |
| 98 | + while(start < end) { |
| 99 | + if(s[start] != s[end]) |
| 100 | + return false; |
| 101 | + ++start, --end; |
| 102 | + } |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + // TC: O(N) |
| 107 | + // SC: O(1) |
| 108 | + bool validPalindrome(string s) { |
| 109 | + for(int i = 0, j = s.size()-1; i < j; ++i, --j) { |
| 110 | + // mismatch found, only if it is the first time delete |
| 111 | + // a char and move on, else not possible |
| 112 | + if(s[i] != s[j]) { |
| 113 | + // s[0:i-1] and s[j+1, n-1] matched, |
| 114 | + // now we check if atleast s[i:j-1] or s[i+1:j] is a palindrome |
| 115 | + return (isPalin(s, i, j-1) || isPalin(s, i+1, j)); |
| 116 | + } |
| 117 | + } |
| 118 | + return true; |
| 119 | + } |
| 120 | +}; |
| 121 | +``` |
| 122 | +Verifying an Alien Dictionary |
| 123 | +```c++ |
| 124 | +class Solution { |
| 125 | +public: |
| 126 | + bool isAlienSorted(vector<string>& words, string order) { |
| 127 | + for (int i = 0; i < words.size() - 1; i++) { |
| 128 | + string word1 = words[i]; |
| 129 | + string word2 = words[i + 1]; |
| 130 | + int i1 = 0, i2 = 0; |
| 131 | + while (word1[i1] == word2[i2]) { |
| 132 | + i1++, i2++; |
| 133 | + } |
| 134 | + int r = order.find(word1[i1]); |
| 135 | + int s = order.find(word2[i2]); |
| 136 | + if (r > s) return false; |
| 137 | + } |
| 138 | + return true; |
| 139 | + } |
| 140 | +}; |
| 141 | +``` |
| 142 | +Container With Most Water |
| 143 | +```c++ |
| 144 | +class Solution { |
| 145 | +public: |
| 146 | + int maxArea(vector<int>& height) { |
| 147 | + int max_a = 0; |
| 148 | + |
| 149 | + int st = 0, end = height.size()-1; |
| 150 | + while (st < end) { |
| 151 | + int water = min(height[st], height[end]) * (end-st); |
| 152 | + max_a = max(max_a, water); |
| 153 | + if (height[st] < height[end]) |
| 154 | + st ++; |
| 155 | + else |
| 156 | + end --; |
| 157 | + } |
| 158 | + |
| 159 | + return max_a; |
| 160 | + } |
| 161 | +}; |
| 162 | +``` |
| 163 | +Find the Duplicate Number |
| 164 | +```c++ |
| 165 | +class Solution { |
| 166 | +public: |
| 167 | + int findDuplicate(vector<int>& nums) { |
| 168 | + int slow, fast; |
| 169 | + slow = fast = 0; |
| 170 | + |
| 171 | + do { |
| 172 | + slow = nums[slow]; |
| 173 | + fast = nums[nums[fast]]; |
| 174 | + } |
| 175 | + while (slow != fast); |
| 176 | + |
| 177 | + slow = 0; |
| 178 | + while (slow != fast) { |
| 179 | + slow = nums[slow]; |
| 180 | + fast = nums[fast]; |
| 181 | + } |
| 182 | + |
| 183 | + return fast; |
| 184 | + } |
| 185 | +}; |
| 186 | +``` |
| 187 | +Rotate a matrix by 90 degree |
| 188 | +```c++ |
| 189 | +class Solution { |
| 190 | +public: |
| 191 | + |
| 192 | + void swap(int *a, int *b) { |
| 193 | + int temp = *a; |
| 194 | + *a = *b; |
| 195 | + *b = temp; |
| 196 | + } |
| 197 | + |
| 198 | + void rotate(vector<vector<int>>& matrix) { |
| 199 | + int rows = matrix.size(); |
| 200 | + int cols = matrix[0].size(); |
| 201 | + |
| 202 | + for (int x = 0; x < rows; x++) { |
| 203 | + for (int y = x + 1; y < cols; y++) { |
| 204 | + swap(&matrix[x][y], &matrix[y][x]); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + for (int x = 0; x < rows; x++) { |
| 209 | + int st = 0, end = cols - 1; |
| 210 | + while (st < end) { |
| 211 | + swap(&matrix[x][st++], &matrix[x][end--]); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | +}; |
| 216 | +``` |
81 | 217 | Divide Two Integers |
82 | 218 | ```c++ |
83 | 219 | class Solution { |
|
0 commit comments