-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_solutions.cpp
More file actions
480 lines (459 loc) · 15.4 KB
/
Copy pathall_solutions.cpp
File metadata and controls
480 lines (459 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include <bits/stdc++.h>
using namespace std;
// ============================================================
// Problem 0001 - Two Sum
// Difficulty : Easy
// Time : O(n)
// Space : O(n)
// ============================================================
class Solution1 {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> seen;
for (int i = 0; i < (int)nums.size(); i++) {
int complement = target - nums[i];
if (seen.count(complement))
return {seen[complement], i};
seen[nums[i]] = i;
}
return {};
}
};
// ============================================================
// Problem 0002 - Add Two Numbers
// Difficulty : Medium
// Time : O(max(m, n))
// Space : O(max(m, n))
// ============================================================
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode* n) : val(x), next(n) {}
};
class Solution2 {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode dummy(0);
ListNode* curr = &dummy;
int carry = 0;
while (l1 || l2 || carry) {
int sum = carry;
if (l1) { sum += l1->val; l1 = l1->next; }
if (l2) { sum += l2->val; l2 = l2->next; }
carry = sum / 10;
curr->next = new ListNode(sum % 10);
curr = curr->next;
}
return dummy.next;
}
};
// ============================================================
// Problem 0003 - Longest Substring Without Repeating Characters
// Difficulty : Medium
// Time : O(n)
// Space : O(min(n, m))
// ============================================================
class Solution3 {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char, int> last;
int maxLen = 0, left = 0;
for (int right = 0; right < (int)s.size(); right++) {
if (last.count(s[right]) && last[s[right]] >= left)
left = last[s[right]] + 1;
last[s[right]] = right;
maxLen = max(maxLen, right - left + 1);
}
return maxLen;
}
};
// ============================================================
// Problem 0004 - Median of Two Sorted Arrays
// Difficulty : Hard
// Time : O(log(min(m, n)))
// Space : O(1)
// ============================================================
class Solution4 {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if (nums1.size() > nums2.size()) swap(nums1, nums2);
int m = nums1.size(), n = nums2.size();
int lo = 0, hi = m;
while (lo <= hi) {
int px = (lo + hi) / 2;
int py = (m + n + 1) / 2 - px;
int maxLX = (px == 0) ? INT_MIN : nums1[px - 1];
int minRX = (px == m) ? INT_MAX : nums1[px];
int maxLY = (py == 0) ? INT_MIN : nums2[py - 1];
int minRY = (py == n) ? INT_MAX : nums2[py];
if (maxLX <= minRY && maxLY <= minRX) {
if ((m + n) % 2 == 0)
return (max(maxLX, maxLY) + min(minRX, minRY)) / 2.0;
return max(maxLX, maxLY);
} else if (maxLX > minRY) hi = px - 1;
else lo = px + 1;
}
return 0.0;
}
};
// ============================================================
// Problem 0005 - Longest Palindromic Substring
// Difficulty : Medium
// Time : O(n^2)
// Space : O(1)
// ============================================================
class Solution5 {
public:
string longestPalindrome(string s) {
int start = 0, maxLen = 1;
auto expand = [&](int l, int r) {
while (l >= 0 && r < (int)s.size() && s[l] == s[r]) { l--; r++; }
if (r - l - 1 > maxLen) { maxLen = r - l - 1; start = l + 1; }
};
for (int i = 0; i < (int)s.size(); i++) {
expand(i, i);
expand(i, i + 1);
}
return s.substr(start, maxLen);
}
};
// ============================================================
// Problem 0006 - Zigzag Conversion
// Difficulty : Medium
// Time : O(n)
// Space : O(n)
// ============================================================
class Solution6 {
public:
string convert(string s, int numRows) {
if (numRows == 1) return s;
vector<string> rows(numRows);
int row = 0, dir = -1;
for (char c : s) {
rows[row] += c;
if (row == 0 || row == numRows - 1) dir = -dir;
row += dir;
}
string result;
for (auto& r : rows) result += r;
return result;
}
};
// ============================================================
// Problem 0007 - Reverse Integer
// Difficulty : Medium
// Time : O(log x)
// Space : O(1)
// ============================================================
class Solution7 {
public:
int reverse(int x) {
int result = 0;
while (x != 0) {
int digit = x % 10;
x /= 10;
if (result > INT_MAX / 10 || result < INT_MIN / 10) return 0;
result = result * 10 + digit;
}
return result;
}
};
// ============================================================
// Problem 0008 - String to Integer (atoi)
// Difficulty : Medium
// Time : O(n)
// Space : O(1)
// ============================================================
class Solution8 {
public:
int myAtoi(string s) {
int i = 0, sign = 1;
long result = 0;
while (i < (int)s.size() && s[i] == ' ') i++;
if (i < (int)s.size() && (s[i] == '-' || s[i] == '+'))
sign = (s[i++] == '-') ? -1 : 1;
while (i < (int)s.size() && isdigit(s[i])) {
result = result * 10 + (s[i++] - '0');
if (result * sign > INT_MAX) return INT_MAX;
if (result * sign < INT_MIN) return INT_MIN;
}
return (int)(result * sign);
}
};
// ============================================================
// Problem 0009 - Palindrome Number
// Difficulty : Easy
// Time : O(log x)
// Space : O(1)
// ============================================================
class Solution9 {
public:
bool isPalindrome(int x) {
if (x < 0 || (x % 10 == 0 && x != 0)) return false;
int reversed = 0;
while (x > reversed) {
reversed = reversed * 10 + x % 10;
x /= 10;
}
return x == reversed || x == reversed / 10;
}
};
// ============================================================
// Problem 0010 - Regular Expression Matching
// Difficulty : Hard
// Time : O(m * n)
// Space : O(m * n)
// ============================================================
class Solution10 {
public:
bool isMatch(string s, string p) {
int m = s.size(), n = p.size();
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));
dp[0][0] = true;
for (int j = 2; j <= n; j++)
if (p[j - 1] == '*') dp[0][j] = dp[0][j - 2];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (p[j - 1] == '*') {
dp[i][j] = dp[i][j - 2] ||
((p[j - 2] == '.' || p[j - 2] == s[i - 1]) && dp[i - 1][j]);
} else if (p[j - 1] == '.' || p[j - 1] == s[i - 1]) {
dp[i][j] = dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}
};
// ============================================================
// Problem 0011 - Container With Most Water
// Difficulty : Medium
// Time : O(n)
// Space : O(1)
// ============================================================
class Solution11 {
public:
int maxArea(vector<int>& height) {
int left = 0, right = (int)height.size() - 1, maxWater = 0;
while (left < right) {
maxWater = max(maxWater, min(height[left], height[right]) * (right - left));
if (height[left] < height[right]) left++;
else right--;
}
return maxWater;
}
};
// ============================================================
// Problem 0012 - Integer to Roman
// Difficulty : Medium
// Time : O(1)
// Space : O(1)
// ============================================================
class Solution12 {
public:
string intToRoman(int num) {
vector<pair<int, string>> table = {
{1000,"M"},{900,"CM"},{500,"D"},{400,"CD"},
{100,"C"},{90,"XC"},{50,"L"},{40,"XL"},
{10,"X"},{9,"IX"},{5,"V"},{4,"IV"},{1,"I"}
};
string result;
for (auto& [val, sym] : table)
while (num >= val) { result += sym; num -= val; }
return result;
}
};
// ============================================================
// Problem 0013 - Roman to Integer
// Difficulty : Easy
// Time : O(n)
// Space : O(1)
// ============================================================
class Solution13 {
public:
int romanToInt(string s) {
unordered_map<char, int> val = {
{'I',1},{'V',5},{'X',10},{'L',50},
{'C',100},{'D',500},{'M',1000}
};
int result = 0;
for (int i = 0; i < (int)s.size(); i++) {
if (i + 1 < (int)s.size() && val[s[i]] < val[s[i + 1]])
result -= val[s[i]];
else
result += val[s[i]];
}
return result;
}
};
// ============================================================
// Problem 0014 - Longest Common Prefix
// Difficulty : Easy
// Time : O(n * m)
// Space : O(1)
// ============================================================
class Solution14 {
public:
string longestCommonPrefix(vector<string>& strs) {
string prefix = strs[0];
for (int i = 1; i < (int)strs.size(); i++)
while (strs[i].find(prefix) != 0)
prefix = prefix.substr(0, prefix.size() - 1);
return prefix;
}
};
// ============================================================
// Problem 0015 - 3Sum
// Difficulty : Medium
// Time : O(n^2)
// Space : O(1)
// ============================================================
class Solution15 {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> result;
for (int i = 0; i < (int)nums.size() - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
int left = i + 1, right = (int)nums.size() - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
result.push_back({nums[i], nums[left], nums[right]});
while (left < right && nums[left] == nums[left + 1]) left++;
while (left < right && nums[right] == nums[right - 1]) right--;
left++; right--;
} else if (sum < 0) left++;
else right--;
}
}
return result;
}
};
// ============================================================
// Problem 0016 - 3Sum Closest
// Difficulty : Medium
// Time : O(n^2)
// Space : O(1)
// ============================================================
class Solution16 {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int closest = nums[0] + nums[1] + nums[2];
for (int i = 0; i < (int)nums.size() - 2; i++) {
int left = i + 1, right = (int)nums.size() - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (abs(sum - target) < abs(closest - target)) closest = sum;
if (sum < target) left++;
else if (sum > target) right--;
else return sum;
}
}
return closest;
}
};
// ============================================================
// Problem 0017 - Letter Combinations of a Phone Number
// Difficulty : Medium
// Time : O(4^n * n)
// Space : O(n)
// ============================================================
class Solution17 {
public:
vector<string> letterCombinations(string digits) {
if (digits.empty()) return {};
unordered_map<char, string> phone = {
{'2',"abc"},{'3',"def"},{'4',"ghi"},{'5',"jkl"},
{'6',"mno"},{'7',"pqrs"},{'8',"tuv"},{'9',"wxyz"}
};
vector<string> result;
string current;
function<void(int)> backtrack = [&](int index) {
if (index == (int)digits.size()) { result.push_back(current); return; }
for (char c : phone[digits[index]]) {
current.push_back(c);
backtrack(index + 1);
current.pop_back();
}
};
backtrack(0);
return result;
}
};
// ============================================================
// Problem 0018 - 4Sum
// Difficulty : Medium
// Time : O(n^3)
// Space : O(1)
// ============================================================
class Solution18 {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
vector<vector<int>> result;
int n = nums.size();
for (int i = 0; i < n - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
for (int j = i + 1; j < n - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) continue;
int left = j + 1, right = n - 1;
while (left < right) {
long long sum = (long long)nums[i] + nums[j] + nums[left] + nums[right];
if (sum == target) {
result.push_back({nums[i], nums[j], nums[left], nums[right]});
while (left < right && nums[left] == nums[left + 1]) left++;
while (left < right && nums[right] == nums[right - 1]) right--;
left++; right--;
} else if (sum < target) left++;
else right--;
}
}
}
return result;
}
};
// ============================================================
// Problem 0019 - Remove Nth Node From End of List
// Difficulty : Medium
// Time : O(n)
// Space : O(1)
// ============================================================
class Solution19 {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode dummy(0, head);
ListNode* fast = &dummy;
ListNode* slow = &dummy;
for (int i = 0; i <= n; i++) fast = fast->next;
while (fast) { fast = fast->next; slow = slow->next; }
ListNode* toDelete = slow->next;
slow->next = slow->next->next;
delete toDelete;
return dummy.next;
}
};
// ============================================================
// Problem 0020 - Valid Parentheses
// Difficulty : Easy
// Time : O(n)
// Space : O(n)
// ============================================================
class Solution20 {
public:
bool isValid(string s) {
stack<char> st;
unordered_map<char, char> match = {{')', '('}, {']', '['}, {'}', '{'}};
for (char c : s) {
if (c == '(' || c == '[' || c == '{') {
st.push(c);
} else {
if (st.empty() || st.top() != match[c]) return false;
st.pop();
}
}
return st.empty();
}
};