Skip to content

Commit 327297f

Browse files
More LEETCODE
1 parent 63fb129 commit 327297f

File tree

6 files changed

+302
-32
lines changed

6 files changed

+302
-32
lines changed

Interview/Algorithm/Array.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
4. Friends Of Appropriate Ages
77
5. Trap Rain Water
88
6. House Robber
9+
7. Degree of an array
10+
8. Decode Ways
911

1012

1113
## Implementation
@@ -300,4 +302,79 @@ public:
300302
return dp[nums.size()-1];
301303
}
302304
};
305+
```
306+
307+
### **Degree of an array**
308+
309+
***Big O:*** O(n) speed, O(n) space
310+
```
311+
Tips:
312+
313+
Hash map to record the number frequency and another map to record the postions of each number. when encounter a number with the same degree, calculate the length and take the minium value of them.
314+
```
315+
```c++
316+
class Solution {
317+
public:
318+
int findShortestSubArray(vector<int>& nums) {
319+
320+
map<int, int> freq;
321+
map<int, vector<int>> pos;
322+
int mx = INT_MIN;
323+
324+
/*
325+
get frequency of each number in array
326+
get highest degree
327+
note the positions of frequencies
328+
*/
329+
for(int i = 0; i < nums.size(); i++)
330+
{
331+
mx = max(mx, ++freq[nums[i]]);
332+
pos[nums[i]].push_back(i);
333+
}
334+
335+
//get shortest distance
336+
int dist = INT_MAX;
337+
for(auto num : nums)
338+
{
339+
if(freq[num] == mx)
340+
dist = min(dist, pos[num].back() - pos[num].front());
341+
}
342+
343+
return dist + 1;
344+
345+
}
346+
};
347+
```
348+
349+
### **Decode ways**
350+
351+
***Big O:*** O(n) speed, O(n) space
352+
```
353+
Tips:
354+
355+
Dynamic programming
356+
```
357+
```c++
358+
class Solution {
359+
public:
360+
int numDecodings(string s) {
361+
// edge cases out - leading zero and single character string
362+
if (s[0] == '0') return 0;
363+
if (s.size() == 1) return 1;
364+
// support variables
365+
int len = s.size(), dp[len];
366+
// preparing dp
367+
dp[0] = 1;
368+
dp[1] = (s[0] == '1' || s[0] == '2' && s[1] < '7' ? 1 : 0) + (s[1] != '0');
369+
for (int i = 2; i < len; i++) {
370+
// edge case: we quit for 2 consecutive zeros
371+
if (s[i] == '0' && (s[i - 1] > '2' || s[i - 1] == '0')) return 0;
372+
// base case: we always keep the previous set of combinations, unless we met a 0
373+
dp[i] = s[i] != '0' ? dp[i - 1] : 0;
374+
// we go and look 2 positions behind if we can make a digit in the 10-26 range
375+
if (s[i - 1] == '1' || s[i - 1] == '2' && s[i] < '7') dp[i] += dp[i - 2];
376+
}
377+
return dp[len - 1];
378+
}
379+
};
303380
```

Interview/Algorithm/LeetCode_for_embedded_advanced.md

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@
7070
21. Friends Of Appropriate Ages v
7171
22. Trap Rain water v
7272
23. House Robber v
73+
24. Degree of an array v
74+
25. Decode Ways v
7375

7476
***Math:***
7577
1. Add Binary v
7678
2. Plus One v
7779
3. Add strings v
7880
4. Fibonacci Number v
79-
5. Pow(x, n)
81+
5. Pow(x, n) v
8082
6. Divide Two Integers v
8183
7. Reverse Integer without long long v
8284
8. Palindrome Number v
@@ -92,42 +94,14 @@
9294
1. Insert Delete GetRandom O(1) v
9395
2. LRU Cache
9496
3. Design Add and Search Words Data Structure
97+
4. Find Median from Data Stream v
9598

9699
***String:***
97100
1. Valid Palindrome II v
101+
2. Valid Palindrome v
102+
3. Decode String v
98103

99104
### Impplementations
100-
Valid Palindrome II
101-
```c++
102-
class Solution {
103-
104-
public:
105-
// Checks if string is a palindrome
106-
bool isPalin(string &s, int start, int end) {
107-
while(start < end) {
108-
if(s[start] != s[end])
109-
return false;
110-
++start, --end;
111-
}
112-
return true;
113-
}
114-
115-
// TC: O(N)
116-
// SC: O(1)
117-
bool validPalindrome(string s) {
118-
for(int i = 0, j = s.size()-1; i < j; ++i, --j) {
119-
// mismatch found, only if it is the first time delete
120-
// a char and move on, else not possible
121-
if(s[i] != s[j]) {
122-
// s[0:i-1] and s[j+1, n-1] matched,
123-
// now we check if atleast s[i:j-1] or s[i+1:j] is a palindrome
124-
return (isPalin(s, i, j-1) || isPalin(s, i+1, j));
125-
}
126-
}
127-
return true;
128-
}
129-
};
130-
```
131105
Verifying an Alien Dictionary
132106
```c++
133107
class Solution {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Problems
2+
3+
1. Insert Delete GetRandom O(1)
4+
2. LRU Cache
5+
3. Design Add and Search Words Data Structure
6+
4. Find Median from Data Stream
7+
8+
9+
## Implementation
10+
11+
### **Find Median from Data Stream**
12+
13+
***Big O:*** O(log(n)) speed, O(n) space
14+
```
15+
Tips:
16+
17+
Two heaps.
18+
```
19+
```c++
20+
class MedianFinder {
21+
priority_queue<int> lo;
22+
priority_queue<int> hi;
23+
24+
public:
25+
// Adds a number into the data structure.
26+
void addNum(int num)
27+
{
28+
lo.push(num); // Add to max heap
29+
30+
hi.push(-lo.top()); // balancing step
31+
lo.pop();
32+
33+
if (lo.size() < hi.size()) { // maintain size property
34+
lo.push(-hi.top());
35+
hi.pop();
36+
}
37+
}
38+
39+
// Returns the median of current data stream
40+
double findMedian()
41+
{
42+
return lo.size() > hi.size() ? lo.top() : ((double) lo.top() - hi.top()) * 0.5;
43+
}
44+
};
45+
```

Interview/Algorithm/math.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
1. Reverse Integer without using long long
44
2. Palindrome Number
55
3. Dot Product of Two Sparse Vectors
6+
4. Pow(x, n)
67

78

89
## Implementation
@@ -102,5 +103,34 @@ public:
102103
return (long long) x == dummy;
103104
}
104105
};
106+
```
107+
108+
## **Pow(x, n)**
105109
110+
***Big O:*** O(log(n)) speed, O(1) space
111+
```
112+
Tips:
113+
114+
Fast Power Algorithm Iterative
115+
```
116+
```c++
117+
class Solution {
118+
public:
119+
double myPow(double x, int n) {
120+
long long N = n;
121+
if (N < 0) {
122+
x = 1 / x;
123+
N = -N;
124+
}
125+
double ans = 1;
126+
double current_product = x;
127+
for (long long i = N; i ; i /= 2) {
128+
if ((i % 2) == 1) {
129+
ans = ans * current_product;
130+
}
131+
current_product = current_product * current_product;
132+
}
133+
return ans;
134+
}
135+
};
106136
```

Interview/Algorithm/string.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
## Problems
2+
3+
1. Valid Palindrome II
4+
2. Valid Palindrome
5+
3. Decode String
6+
7+
## Implementation
8+
9+
### **Valid Palindrome**
10+
11+
***Big O:*** O(n), O(1) space
12+
```
13+
Tips:
14+
15+
Two pointers.
16+
```
17+
```c++
18+
class Solution {
19+
public:
20+
bool isPalindrome(string s) {
21+
int st = 0;
22+
int end = s.size()-1;
23+
24+
while (st < end) {
25+
if (!isalnum(s[st]) || !isalnum(s[end])) {
26+
while(st < end && !isalnum(s[st])) {
27+
st ++;
28+
}
29+
while(st < end && !isalnum(s[end])) {
30+
end --;
31+
}
32+
} else {
33+
if ((isalpha(s[st]) && isalpha(s[end]) && tolower(s[st]) == tolower(s[end])) || (s[st] - '0' == s[end] - '0')) {
34+
st ++;
35+
end --;
36+
} else
37+
return false;
38+
}
39+
}
40+
41+
return true;
42+
}
43+
};
44+
```
45+
### **Valid Palindrome II **
46+
47+
***Big O:*** O(n), O(1) space
48+
```
49+
Tips:
50+
51+
Implement is_palindrome first. return the answer of two is_palin or together.
52+
```
53+
```c++
54+
class Solution {
55+
56+
public:
57+
// Checks if string is a palindrome
58+
bool isPalin(string &s, int start, int end) {
59+
while(start < end) {
60+
if(s[start] != s[end])
61+
return false;
62+
++start, --end;
63+
}
64+
return true;
65+
}
66+
67+
// TC: O(N)
68+
// SC: O(1)
69+
bool validPalindrome(string s) {
70+
for(int i = 0, j = s.size()-1; i < j; ++i, --j) {
71+
// mismatch found, only if it is the first time delete
72+
// a char and move on, else not possible
73+
if(s[i] != s[j]) {
74+
// s[0:i-1] and s[j+1, n-1] matched,
75+
// now we check if atleast s[i:j-1] or s[i+1:j] is a palindrome
76+
return (isPalin(s, i, j-1) || isPalin(s, i+1, j));
77+
}
78+
}
79+
return true;
80+
}
81+
};
82+
```
83+
84+
### **Decode String**
85+
86+
***Big O:***
87+
88+
Time Complexity: O(maxK⋅n), where maxK is the maximum value of kk and nn is the length of a given string ss. We traverse a string of size nn and iterate kk times to decode each pattern of form k[string]. This gives us worst case time complexity as O(maxK⋅n).
89+
90+
Space Complexity: O(m+n), where mm is the number of letters(a-z) and nn is the number of digits(0-9) in string ss. In worst case, the maximum size of stringStack and countStack could be mm and nn respectively.
91+
```
92+
Tips:
93+
94+
Use stack to store pair<int, int>(frequency, string). (Equivalent of using two stacks).
95+
```
96+
```c++
97+
class Solution {
98+
public:
99+
string decodeString(string s) {
100+
stack<pair<int, string>> decode_stk;
101+
string ret = "";
102+
103+
int number = 0;
104+
for (int i = 0; i < s.size(); i++) {
105+
if (s[i] == '[') {
106+
string encode_str = "";
107+
decode_stk.push(make_pair(number, encode_str));
108+
number = 0;
109+
} else if (s[i] == ']') {
110+
pair<int, string> tmp = decode_stk.top();
111+
decode_stk.pop();
112+
113+
string copy = tmp.second;
114+
while(--tmp.first) {
115+
copy += tmp.second;
116+
}
117+
118+
if (decode_stk.empty()) {
119+
ret += copy;
120+
} else {
121+
tmp = decode_stk.top();
122+
decode_stk.pop();
123+
tmp.second += copy;
124+
decode_stk.push(tmp);
125+
}
126+
} else if (isalpha(s[i])) {
127+
if (decode_stk.empty()) {
128+
ret += s[i];
129+
} else {
130+
pair<int, string> tmp = decode_stk.top();
131+
decode_stk.pop();
132+
tmp.second += s[i];
133+
decode_stk.push(tmp);
134+
}
135+
} else if (isdigit(s[i])) {
136+
number = number*10 + s[i] - '0';
137+
}
138+
}
139+
140+
return ret;
141+
}
142+
};
143+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
6. [Binary Search Tree](Data_Struct_Implementation/BST/README.md)
6767
7. Red Black Tree
6868
8. Minium Spanning Tree (MST)
69+
9. Bitwise tries
6970
10. Math
7071
1. Rolling average
7172
2. Taylor Series

0 commit comments

Comments
 (0)