Skip to content

Commit 516f407

Browse files
More leetcode
1 parent 90c9d85 commit 516f407

File tree

6 files changed

+299
-8
lines changed

6 files changed

+299
-8
lines changed

Interview/Algorithm/Array.md

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
10. Monotonic Array
1313
11. Longest Continuous Increasing Subsequence
1414
12. Maximum Product Subarray
15-
15+
13. First Missing Positive
16+
14. Maximum Swap
17+
15. Meeting Room II
18+
16. Sort Colors
1619

1720
## Implementation
1821

@@ -455,7 +458,6 @@ public:
455458
}
456459
};
457460
```
458-
459461
### **Maximum Product Subarray**
460462

461463
***Big O:*** O(n) speed, O(1) space
@@ -488,4 +490,135 @@ public:
488490
return result;
489491
}
490492
};
493+
```
494+
495+
### **First Missing Positive**
496+
497+
***Big O:*** O(n) speed, O(1) space
498+
```
499+
Tips:
500+
501+
Hashmap.
502+
```
503+
```c++
504+
class Solution {
505+
public:
506+
int firstMissingPositive(vector<int>& nums) {
507+
unordered_map<int, int> umap;
508+
int max_v = 0;
509+
510+
for (int i = 0; i < nums.size(); i++) {
511+
if (nums[i] > 0) {
512+
umap[nums[i]] = 1;
513+
max_v = max(max_v, nums[i]);
514+
}
515+
}
516+
517+
for (int i = 1; i < max_v; i++) {
518+
if (umap.find(i) == umap.end())
519+
return i;
520+
}
521+
522+
return max_v+1;
523+
}
524+
};
525+
```
526+
527+
### **Maximum Swap**
528+
529+
***Big O:*** O(n) speed, O(1) space
530+
```
531+
Tips:
532+
533+
Greedy.
534+
```
535+
```c++
536+
class Solution {
537+
public:
538+
int maximumSwap(int num) {
539+
string num_str = to_string(num);
540+
int n = num_str.size();
541+
int max_val = -1, max_i = -1;
542+
int left = -1, right = -1;
543+
544+
for (int i = n - 1; i >= 0; i--) {
545+
if (num_str[i] > max_val) {
546+
max_val = num_str[i];
547+
max_i = i;
548+
} else if (num_str[i] < max_val) {
549+
left = i;
550+
right = max_i;
551+
}
552+
}
553+
554+
if (left == -1) return num;
555+
char c = num_str[left];
556+
num_str[left] = num_str[right];
557+
num_str[right] = c;
558+
return stoi(num_str);
559+
}
560+
};
561+
```
562+
563+
### **Meeting Rooms II**
564+
565+
***Big O:*** O(nlog(n)) speed, O(n) space
566+
```
567+
Tips:
568+
569+
Sort + priority queue. Kick people out if their meeting is already finished.
570+
```
571+
```c++
572+
class Solution {
573+
public:
574+
int minMeetingRooms(vector<vector<int>>& intervals) {
575+
sort(intervals.begin(), intervals.end());
576+
577+
priority_queue<int, vector<int>, greater<int>> pq;
578+
579+
for (auto v : intervals) {
580+
// check if the last meetings need to be completed
581+
// before this meeting starts.
582+
if (!pq.empty() && pq.top() <= v[0]) {
583+
pq.pop();
584+
}
585+
586+
// start this meeting and enter the end time.
587+
pq.push(v[1]);
588+
}
589+
590+
return pq.size();
591+
}
592+
};
593+
```
594+
595+
### **Sort Colors**
596+
597+
***Big O:*** O(n) speed (one pass), O(1) space
598+
```
599+
Tips:
600+
601+
The idea of solution is to move curr pointer along the array, if nums[curr] = 0 - swap it with nums[p0], if nums[curr] = 2 - swap it with nums[p2].
602+
```
603+
```c++
604+
class Solution {
605+
606+
public:
607+
void sortColors(vector<int>& nums) {
608+
int lo = 0, hi = nums.size() - 1, i = 0;
609+
610+
while (i <= hi) {
611+
if (nums[i] == 0) swap(nums, lo++, i++);
612+
else if (nums[i] == 2) swap(nums, i, hi--);
613+
else if (nums[i] == 1) i++;
614+
}
615+
}
616+
617+
void swap(vector<int>& nums, int i, int j) {
618+
int t = nums[i];
619+
nums[i] = nums[j];
620+
nums[j] = t;
621+
}
622+
623+
};
491624
```

Interview/Algorithm/LeetCode_for_embedded_advanced.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
18. Add Two Numbers v
2323
19. Add Two Numbers II v
2424
20. Linked List Insetion Sort v
25+
21. Reorder List v
2526

2627
***Bit Manipulation:***
2728
1. Reverse Bits v
@@ -76,6 +77,10 @@
7677
27. Monotonic Array v
7778
28. Longest Continuous Increasing Subsequence v
7879
29. Maximum Product Subarray v
80+
30. First Missing Positive v
81+
31. Maximum Swap v
82+
32. Meeting Room II v
83+
33. Sort Colors v
7984

8085
***Math:***
8186
1. Add Binary v
@@ -94,6 +99,7 @@
9499
2. Rotate a matrix by 90 degree v
95100
3. Sparse Matrix Multiplication
96101
4. Search 2D Matrix
102+
5. Diagonal Traverse v
97103

98104
***Data Structure:***
99105
1. Insert Delete GetRandom O(1) v
@@ -105,6 +111,7 @@
105111
1. Valid Palindrome II v
106112
2. Valid Palindrome v
107113
3. Decode String v
114+
4. Find All Anagrams in a String v
108115

109116
### Impplementations
110117
Verifying an Alien Dictionary

Interview/Algorithm/linked_list.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Linked list problems
1+
## Problems
22

33
Index # | Title | Diffculty | Importance/Frequency
44
----|----|----|---
@@ -15,3 +15,52 @@ Index # | Title | Diffculty | Importance/Frequency
1515
11 | LRU cache | Hard | ****
1616
12 | Middle of linked list | Easy | ****
1717
13 | Implement queue by linked list | Easy | ****
18+
14 | Reorder List | Medium | ****
19+
20+
## Implementation
21+
22+
### **Reorder List**
23+
24+
***Big O:*** O(n) speed, O(1) space
25+
```
26+
Tips:
27+
28+
1. Find a Middle Node.
29+
2. Reverse the Second Part of the List.
30+
3. Merge Two Sorted Lists.
31+
```
32+
```c++
33+
class Solution {
34+
public:
35+
void reorderList(ListNode* head) {
36+
if ( ! head ) return;
37+
ListNode *slow = head, *fast = head;
38+
while ( fast->next && fast->next->next )
39+
{
40+
slow = slow->next;
41+
fast = fast->next->next;
42+
}
43+
44+
45+
ListNode *prev = NULL, *cur = slow->next, *save;
46+
while ( cur )
47+
{
48+
save = cur->next;
49+
cur->next = prev;
50+
prev = cur;
51+
cur = save;
52+
}
53+
54+
slow->next = NULL;
55+
56+
ListNode *head2 = prev;
57+
while ( head2 )
58+
{
59+
save = head->next;
60+
head->next = head2;
61+
head = head2;
62+
head2 = save;
63+
}
64+
}
65+
};
66+
```

Interview/Algorithm/matrix.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Problems
2+
3+
1. Diagonal Traverse
4+
5+
6+
## Implementation
7+
8+
### **Diagonal Traverse**
9+
10+
***Big O:*** O(n*m) speed, O(min(n,m)) space
11+
```
12+
Tips:
13+
14+
```
15+
```c++
16+
class Solution {
17+
public:
18+
vector<int> findDiagonalOrder(vector<vector<int>>& matrix) {
19+
int rows = matrix.size();
20+
if (rows == 0)
21+
return vector<int>();
22+
23+
int cols = matrix[0].size();
24+
if (cols == 0)
25+
return vector<int>();
26+
27+
int dig_size = rows + cols - 1;
28+
29+
vector<int> ret;
30+
int i = 0, j = 0;
31+
for (int k = 1; k <= dig_size; k++) {
32+
vector<int> diag{};
33+
i = k >= rows ? rows-1 : k-1;
34+
j = k >= rows ? k-rows : 0;
35+
while(i >= 0 && j < cols) {
36+
diag.push_back(matrix[i][j]);
37+
i--;
38+
j++;
39+
}
40+
if (k%2 == 0)
41+
reverse(diag.begin(), diag.end());
42+
ret.insert(ret.end(), diag.begin(), diag.end());
43+
}
44+
45+
return ret;
46+
}
47+
};
48+
```

Interview/Algorithm/string.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
1. Valid Palindrome II
44
2. Valid Palindrome
5-
3. Decode String
5+
3. Decode String
6+
4. Find All Anagrams in a String
67

78
## Implementation
89

@@ -140,4 +141,56 @@ public:
140141
return ret;
141142
}
142143
};
144+
```
145+
### **Find All Anagrams in a String**
146+
147+
***Big O:*** O(Ns + Np), O(1) space (no more than 26 characters)
148+
```
149+
Tips:
150+
151+
Sliding Window with HashMap or Sliding Window with Array with 26 entries
152+
```
153+
```c++
154+
class Solution {
155+
public:
156+
vector<int> findAnagrams(string s, string p) {
157+
unordered_map<char, int> p_umap, s_umap;
158+
vector<int> ret;
159+
160+
if (s.size() < p.size())
161+
return ret;
162+
163+
for (auto c : p)
164+
p_umap[c] ++;
165+
166+
int st = 0, end = 0;
167+
168+
while (end < s.size()) {
169+
//cout << st << end << endl;
170+
if (end-st+1 < p.size()) {
171+
if (p_umap.find(s[end]) != p_umap.end()) {
172+
s_umap[s[end]] ++;
173+
end ++;
174+
} else {
175+
s_umap.clear();
176+
end ++;
177+
st = end;
178+
}
179+
} else if ((end-st+1 > p.size())) {
180+
if (s_umap[s[st]] == 1)
181+
s_umap.erase(s[st]);
182+
else
183+
s_umap[s[st]] --;
184+
st ++;
185+
} else {
186+
s_umap[s[end]] ++;
187+
if (s_umap == p_umap)
188+
ret.push_back(st);
189+
end ++;
190+
}
191+
}
192+
193+
return ret;
194+
}
195+
};
143196
```

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,12 @@
305305
## Interview Questions
306306

307307
### [A. LeetCode Questions for Embedded Developers](Interview/Algorithm/LeetCode_for_Embedded_Developer.md)
308-
1. Bitwise Operations
308+
1. [Advanced Leetcode Questions](Interview/Algorithm/LeetCode_for_embedded_advanced.md)
309309
2. [Linked list](Interview/Algorithm/linked_list.md)
310-
3. String
311-
4. Array
312-
5. Math
310+
3. [String]((Interview/Algorithm/string.md))
311+
4. [Array](Interview/Algorithm/Array.md)
312+
5. [Math](Interview/Algorithm/math.md)
313+
6. [Data Structure](Interview/Algorithm/dataStructure.md)
313314

314315
### B. Other Algorithms Questions
315316
1. Sliding Window

0 commit comments

Comments
 (0)