Skip to content

Commit 06c89d8

Browse files
More algorthim
1 parent b8b34f0 commit 06c89d8

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

Interview/Company/facebook/algorithm_prepare.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
15. Intersection of Two Arrays II Easy
1919
16. Sparse Matrix Multiplication Medium
2020
17. Rectangle Overlap Easy
21+
18. Minimum Window Substring Hard
22+
19. Valid Number Hard
2123

2224
## Binary Search
2325
1. Search a 2D Matrix Easy
@@ -31,6 +33,8 @@
3133
1. Reverse Linked List II Medium
3234
2. Merge K sorted List Medium
3335

36+
## String
37+
1. Valid Anagram Easy
3438

3539

3640
## Implementation
@@ -1079,4 +1083,134 @@ public:
10791083
return (min(r1.x, r2.x) > max(l1.x, l2.x)) && (min(l1.y, l2.y) > max(r1.y, r2.y));
10801084
}
10811085
};
1086+
```
1087+
1088+
### **Rectangle Overlap**
1089+
1090+
***Big O:*** O(K + T) speed, O(1) space, K size of s, T size of T
1091+
```
1092+
Tips:
1093+
1094+
Algorithm
1095+
1096+
We start with two pointers, leftleft and rightright initially pointing to the first element of the string SS.
1097+
1098+
We use the rightright pointer to expand the window until we get a desirable window i.e. a window that contains all of the characters of TT.
1099+
1100+
Once we have a window with all the characters, we can move the left pointer ahead one by one. If the window is still a desirable one we keep on updating the minimum window size.
1101+
1102+
If the window is not desirable any more, we repeat step \; 2step2 onwards.
1103+
```
1104+
```c++
1105+
1106+
class Solution {
1107+
1108+
public:
1109+
static string minWindow(const string &str, const string &pattern) {
1110+
// TODO: Write your code here
1111+
string ret_str = "";
1112+
unsigned int min_str = -1, win_s, win_e, match = 0;
1113+
unordered_map <char, int> umap;
1114+
1115+
for (auto chr : pattern)
1116+
umap[chr] ++;
1117+
1118+
for (win_e = win_s = 0; win_e < str.size(); win_e ++) {
1119+
if (umap.find(str[win_e]) != umap.end()) {
1120+
if (--umap[str[win_e]] == 0) {
1121+
match ++;
1122+
}
1123+
}
1124+
1125+
while (match == umap.size()) {
1126+
if ((win_e - win_s + 1) < min_str) {
1127+
min_str = min(min_str, win_e - win_s + 1);
1128+
ret_str = str.substr(win_s, win_e - win_s + 1);
1129+
}
1130+
1131+
if (umap.find(str[win_s]) != umap.end() && ++umap[str[win_s]] > 0) {
1132+
match --;
1133+
}
1134+
1135+
win_s++;
1136+
}
1137+
}
1138+
1139+
return ret_str;
1140+
}
1141+
};
1142+
```
1143+
### **Valid Number**
1144+
1145+
***Big O:*** O(N) speed, O(1) space
1146+
```
1147+
Tips:
1148+
1149+
Divide and conquer. Divide it into two parts around the e/E symbol and check both sides.
1150+
```
1151+
```c++
1152+
class Solution {
1153+
public:
1154+
bool ojbk(string s, int k){
1155+
for(int i=0;i<s.size();i++){
1156+
if((s[i]=='+' || s[i]=='-') && i!=0) return false;
1157+
}
1158+
if(s[0]=='+' || s[0]=='-') s.erase(s.begin());
1159+
int countDot=0;
1160+
for(int i=0;i<s.size();i++){
1161+
if(s[i]=='.') countDot++;
1162+
else if(!isdigit(s[i])) return false;
1163+
}
1164+
if(s=="" || s==".") return false;
1165+
1166+
return countDot<=k;
1167+
}
1168+
bool isNumber(string s) {
1169+
while(s.size()>0 && s.back()==' ') s.pop_back();
1170+
while(s.size()>0 && s[0]==' ') s.erase(s.begin());
1171+
int countE=0;
1172+
int pos;
1173+
for(int i=0;i<s.size();i++){
1174+
if(s[i]=='e'){
1175+
countE++;
1176+
pos=i;
1177+
}
1178+
}
1179+
if(countE>=2) return false;
1180+
if(countE==0) return ojbk(s,1);
1181+
return ojbk(s.substr(0,pos),1) && ojbk(s.substr(pos+1),0);
1182+
}
1183+
};
1184+
```
1185+
1186+
### **Valid Anagram**
1187+
1188+
***Big O:*** O(N) speed, O(1) space
1189+
```
1190+
Tips:
1191+
1192+
Hash map.
1193+
```
1194+
```c++
1195+
class Solution {
1196+
public:
1197+
bool isAnagram(string s, string t) {
1198+
int umap[26] = {};
1199+
1200+
if (s.size() != t.size())
1201+
return false;
1202+
1203+
for (auto &c : s)
1204+
umap[c-'a'] ++;
1205+
1206+
int count = 0;
1207+
for (auto &c : t) {
1208+
umap[c-'a'] --;
1209+
if (umap[c-'a'] < 0)
1210+
return false;
1211+
}
1212+
1213+
return true;
1214+
}
1215+
};
10821216
```

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@
214214
3. [IDT problems](https://wiki.osdev.org/IDT_problems#Problems_with_IDTs)
215215
4. [Advanced Programmable Interrupt Controller (APIC)](https://wiki.osdev.org/APIC)
216216
4. [Memory Hierachy](https://people.freebsd.org/~lstewart/articles/cpumemory.pdf)
217-
5. [Cache coherence](https://www.geeksforgeeks.org/cache-coherence/)
217+
5. Memory
218+
1. [Cache memory](https://www.geeksforgeeks.org/cache-memory-in-computer-organization/)
219+
2. [Cache coherence](https://www.geeksforgeeks.org/cache-coherence/)
218220
6. [DMA](./Computer_architecture/dma.md)
219221
7. TCM
220222
8. [TLB](./Computer_architecture/tlb.md)

0 commit comments

Comments
 (0)