Skip to content

Commit d9f1196

Browse files
more leetcode
1 parent 6a684f8 commit d9f1196

File tree

2 files changed

+139
-2
lines changed

2 files changed

+139
-2
lines changed

Interview/Algorithm/LeetCode_for_embedded_advanced.md

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
12. Maximum Number of Occurrences of a Substring v
6262
13. Maximum Subarray v
6363
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
6467

6568
***Math:***
6669
1. Add Binary v
@@ -72,12 +75,145 @@
7275

7376
***Matirx:***
7477
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
7581

7682
***Data Structure:***
7783
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
7989

8090
### 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+
```
81217
Divide Two Integers
82218
```c++
83219
class Solution {

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@
9191
3. Given a matrix and its center coordinates, draw the circle
9292
4. Find a 32 bit frame start sequence in a raw byte stream buffer
9393
5. Memory tracker to hook into glibc library to keep track of dynamic memory allocations, including statistics
94-
19. [Other Common C algorithm](https://github.com/fragglet/c-algorithms)
94+
6. Given an array of angles and an array of voltages for these angles, now there is a new angle coming in, calculate its voltage.
95+
1. [Other Common C algorithm](https://github.com/fragglet/c-algorithms)
9596

9697

9798

0 commit comments

Comments
 (0)