§Summer Lec 4: Special Data Structures
§problem
§Flatten 2D Vector
1 | class Vector2D { |
§Follow up
Java:
runtiome Exception/unchecked, eg. throw new NoSuchElementException()
checked Exception -> catch or declare
Uber interview, Implement Token Bucket. eg.sleep throws InterruptedException
remove()
§Iterator Design Pattern in Java
implement next(), hasNext(), remove()
good for Lazy Loading: File I/O, DAO, Big Data
§pancake sort
§Max Chunks To Make Sorted, Max Chunks To Make Sorted II
§shallow copy / deep copy
1 | //sol 2 |
§76. Minimum Window Substring
§380. Insert Delete GetRandom O(1), 381. Insert Delete GetRandom O(1) - Duplicates allowed
Load Balancer: Randomly choose a server. Runder Robin
Array: O(1) getRandom
HashMap: O(1) insert and remove
1 | vector<int> nums; |
§Summer Lec 5: DP & Airbnb
§Dynamic programming
- Deduction formula
- Initialization
- Space improvement
§problem
§Edit Distance
#####Airbnb interview questions
-
K Edit Distance, KEditDistance.java: Trie + DP
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
74class TrieNode{
public:
vector<TrieNode*> child;
bool isEnd;
string word;
TrieNode(){
child.resize(26);
isEnd = false;
word = "";
}
~TrieNode(){
for (TrieNode *p : child)
if (p)
delete p;
}
};
class Solution {
private:
void insert(string& word, TrieNode* root){
TrieNode* cur = root;
for (char c:word){
int i = c - 'a';
if (cur->child[i] == NULL)
cur->child[i] = new TrieNode();
cur = cur->child[i];
}
cur->isEnd = true;
cur->word = word;
}
void dfs(TrieNode* cur, string& target, int k, vector<int>& dp, vector<string>& res){
int l = target.length();
if (cur->isEnd && dp[l] <= k)
res.push_back(cur->word);
vector<int> nextdp(l+1);
nextdp[0] = dp[0]+1;
for (int i = 0; i < 26; ++i)
if (cur->child[i]){
for (int j = 1; j <= l; ++j)
//nextdp[j]: target j-1 ~ trie with c
if (target[j-1]-'a' == i)
//dp[j-1]: taret j-2 ~ trie without c
nextdp[j] = dp[j-1];
else
//taret j-2 ~ trie without c ; j-1 ~ without c, j-2 ~ with c
//add c; replace one letter, convert c ~ target[j-1]; add target[j-1]
nextdp[j] = min(min(dp[j-1],dp[j]),nextdp[j-1])+1;
dfs(cur->child[i], target, k, nextdp, res);
}
}
public:
/**
* @param words: a set of stirngs
* @param target: a target string
* @param k: An integer
* @return: output all the strings that meet the requirements
*/
vector<string> kDistance(vector<string> &words, string &target, int k) {
TrieNode* root = new TrieNode();
int l = target.length();
for (string& word:words){
//prune
//if (word.length() <= l+k && word.length() >= l-k)
insert(word,root);
}
vector<int> dp(l+1);
iota(dp.begin(),dp.end(),0);
vector<string> res;
dfs(root, target, k, dp, res);
delete root;
return res;
}
};
-
Cheapest Flights within K Stops, CheapestFlightsWithinKStops.java
- Follow up: output the route
§221. Maximal Square
§85. Maximal Rectangle, 764. Largest Plus Sign
§stack/string : Basic Calculator
§224. Basic Calculator, 227. Basic Calculator II
§772. Basic Calculator III
1 | class Solution { |