Skip to content

Commit 5f09c4d

Browse files
author
kaidul
committed
Bloomberg interview ahead..
1 parent 23ba283 commit 5f09c4d

13 files changed

+689
-657
lines changed

README.md

+594-593
Large diffs are not rendered by default.

source-code/Add_Two_Numbers_II.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,8 @@ class Solution {
8686
int carry = 0;
8787
ListNode* head = new ListNode(0);
8888
ListNode* curr = head;
89-
if(diff >= 0) {
90-
addTwoNumbers(l1, diff, l2, curr, head, carry);
91-
} else {
92-
addTwoNumbers(l2, -diff, l1, curr, head, carry);
93-
}
89+
if(diff < 0) swap(l1, l2);
90+
addTwoNumbers(l1, abs(diff), l2, curr, head, carry);
9491
if(carry) {
9592
ListNode* sentinel = new ListNode(carry);
9693
sentinel->next = curr;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
void allPathsSourceTarget(int curr,
3+
int const dest,
4+
vector<vector<int>> const& graph,
5+
vector<bool>& visited,
6+
vector<int>& solution, vector<vector<int>>& result) {
7+
if(curr == dest) {
8+
result.push_back(solution);
9+
return;
10+
}
11+
for(int i = 0; i < graph[curr].size(); i++) {
12+
int neigh = graph[curr][i];
13+
if(!visited[neigh]) {
14+
visited[neigh] = true;
15+
solution.push_back(neigh);
16+
allPathsSourceTarget(neigh, dest, graph, visited, solution, result);
17+
visited[neigh] = false;
18+
solution.pop_back();
19+
}
20+
}
21+
}
22+
public:
23+
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
24+
int n = (int) graph.size();
25+
int src = 0, dest = n - 1;
26+
vector<vector<int>> result;
27+
vector<bool> visited(n, false);
28+
vector<int> solution;
29+
solution.push_back(src);
30+
visited[src] = true;
31+
allPathsSourceTarget(src, dest, graph, visited, solution, result);
32+
33+
return result;
34+
}
35+
};

source-code/Degree_of_an_Array.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Solution {
2424
}
2525
};
2626

27-
// using tow pointers (complex)
27+
// using tow pointers
2828
class Solution {
2929
public:
3030
int findShortestSubArray(vector<int>& nums) {
@@ -56,12 +56,6 @@ class Solution {
5656
freq2[nums[start]]--;
5757
start++;
5858
maxFreq2--;
59-
auto last = freq2.end();
60-
--last;
61-
if(last->second > maxFreq2) {
62-
maxFreq2 = last->second;
63-
curr = last->first;
64-
}
6559
}
6660
}
6761

source-code/Elimination_Game.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
int leftToRight(int n) {
3-
if(n <= 2) return n;
3+
if(n == 1) return n;
44
return 2 * rightToLeft(n / 2);
55
}
66

77
int rightToLeft(int n) {
8-
if(n <= 2) return 1;
8+
if(n == 1) return n;
99
if(n % 2 == 1) return 2 * leftToRight(n / 2);
1010
return 2 * leftToRight(n / 2) - 1;
1111
}

source-code/LFU_Cache.cpp

+22-23
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ class LFUCache {
5252
delete chain;
5353
}
5454

55-
Chain* removeNode(int key) {
55+
void removeNode(int key) {
5656
Chain* chain = Map[key].first;
5757
ListNode* node = Map[key].second;
5858
Map.erase(key);
59+
5960
if(node->prev) {
6061
node->prev->next = node->next;
6162
} else {
@@ -67,12 +68,13 @@ class LFUCache {
6768
chain->tail = node->prev;
6869
}
6970
delete node;
70-
71-
Chain* newChain = nullptr;
71+
}
72+
73+
Chain* updateChain(Chain* chain) {
7274
if(!chain->next or chain->next->freq > chain->freq + 1) {
7375
chain->next = new Chain(chain);
7476
}
75-
newChain = chain->next;
77+
Chain* newChain = chain->next;
7678

7779
if(!chain->head and !chain->tail) {
7880
removeChain(chain);
@@ -81,9 +83,8 @@ class LFUCache {
8183
return newChain;
8284
}
8385

84-
void setHead(int key) {
85-
Chain* chain = Map[key].first;
86-
ListNode* node = Map[key].second;
86+
void setHead(Chain* chain, ListNode* node) {
87+
Map[node->key] = {chain, node};
8788
node->next = chain->head;
8889
node->prev= nullptr;
8990
if(chain->head) {
@@ -104,7 +105,7 @@ class LFUCache {
104105

105106
LFUCache(int capacity) {
106107
this->capacity = capacity;
107-
head = nullptr;
108+
head = new Chain();
108109
Map = unordered_map<int, pair<Chain*, ListNode*> >();
109110
currLen = 0;
110111
}
@@ -113,49 +114,47 @@ class LFUCache {
113114
if(Map.find(key) == Map.end()) {
114115
return -1;
115116
}
117+
Chain* chain = Map[key].first;
116118
ListNode* node = Map[key].second;
117119
int value = node->value;
118-
Chain* newChain = removeNode(key);
120+
removeNode(key);
121+
Chain* newChain = updateChain(chain);
119122
ListNode* newNode = new ListNode(key, value);
120-
Map[key] = {newChain, newNode};
121-
setHead(key);
123+
setHead(newChain, newNode);
122124

123125
return value;
124126
}
125127

126-
void set(int key, int value) {
128+
void put(int key, int value) {
127129

128130
if(capacity == 0) {
129131
return;
130132
}
131133

134+
ListNode* newNode = new ListNode(key, value);
135+
132136
if(Map.find(key) != Map.end()) {
133-
ListNode* node = Map[key].second;
134-
Chain* newChain = removeNode(key);
135-
ListNode* newNode = new ListNode(key, value);
136-
Map[key] = {newChain, newNode};
137-
setHead(key);
137+
Chain* chain = Map[key].first;
138+
removeNode(key);
139+
Chain* newChain = updateChain(chain);
140+
setHead(newChain, newNode);
138141
return;
139142
}
140143

141144
if(currLen == capacity) {
142145
Chain* chain = head;
143146
ListNode* node = chain->tail;
144147
removeNode(node->key);
148+
updateChain(chain);
145149
currLen--;
146150
}
147-
ListNode* newNode = new ListNode(key, value);
148-
if(!head) {
149-
head = new Chain();
150-
}
151151
if(head->freq > 0) {
152152
Chain* newHead = new Chain();
153153
newHead->next = head;
154154
head->prev = newHead;
155155
head = newHead;
156156
}
157-
Map[key] = {head, newNode};
158-
setHead(key);
157+
setHead(head, newNode);
159158

160159
currLen++;
161160
}

source-code/Longest_Increasing_Subsequence.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ class Solution {
44
int n = nums.size();
55
if(n == 0) return 0;
66
vector<int> I(n + 1, numeric_limits<int>::max());
7-
vector<int> L(n, 0);
87
I[0] = numeric_limits<int>::min();
98
int length = 0;
109
for(int i = 0; i < n; ++i) {

source-code/Moving_Average_from_Data_Stream.cpp

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
class MovingAverage {
2-
vector<int> nums;
3-
int n;
4-
int k;
5-
int m;
2+
deque<int> window;
3+
int sum;
4+
int maxLength;
65
public:
76
/** Initialize your data structure here. */
87
MovingAverage(int size) {
9-
n = size;
10-
nums.clear();
11-
nums.resize(n, 0);
12-
k = 0; m = 0;
8+
sum = 0;
9+
maxLength = size;
10+
window = deque<int>();
1311
}
1412

1513
double next(int val) {
16-
nums[k++] = val;
17-
if(k == n) k = 0;
18-
if(m < n) ++m;
19-
double sum = 0.0;
20-
for(int i = 0; i < m; ++i) {
21-
sum += nums[i];
14+
sum += val;
15+
window.push_back(val);
16+
if(window.size() > maxLength) {
17+
sum -= window.front();
18+
window.pop_front();
2219
}
23-
double avg = sum / (1.0 * m);
24-
25-
return avg;
20+
return sum / (1.0 * window.size());
2621
}
2722
};
2823

source-code/Non-decreasing_Array.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Solution {
99
}
1010
if(i - 1 >= 0) {
1111
if(nums[i + 1] >= nums[i - 1]) {
12-
nums[i] = nums[i - 1];
12+
nums[i] = nums[i - 1]; // or nums[i] = nums[i + 1];
1313
} else if(nums[i + 1] < nums[i - 1]) {
1414
nums[i + 1] = nums[i];
1515
}

source-code/Sort_Characters_By_Frequency.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class Solution {
2020
}
2121
}
2222
#endif
23-
// Using maxHeap (faster)
24-
auto compare = [] (pair<int, int> const& lhs, pair<int, int> const& rhs) -> bool const { return lhs.first < rhs.first; };
25-
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(compare)> Q(compare);
23+
// auto compare = [] (pair<int, int> const& lhs, pair<int, int> const& rhs) -> bool const { return lhs.first < rhs.first; };
24+
// priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(compare)> Q(compare);
25+
priority_queue<pair<int, int>> Q;
2626
for(int i = 0 ; i < MAX_CHAR; ++i) {
2727
if(freq[i] > 0) {
2828
Q.push({freq[i], i});

source-code/Subtree_of_Another_Tree.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Solution {
2121
}
2222
};
2323

24-
// using preorder hash
24+
// using preorder hash (postorder or inorder also works)
2525
class Solution {
2626
string preorderHash(TreeNode* root) {
2727
if(!root) {

source-code/Sum_of_Left_Leaves.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,18 @@ class Solution {
1717
int sumOfLeftLeaves(TreeNode* root) {
1818
return sumOfLeftLeavesRecur(root, false);
1919
}
20+
};
21+
22+
// Another approach without any helper method
23+
class Solution {
24+
public:
25+
int sumOfLeftLeaves(TreeNode* root) {
26+
if(!root) return 0;
27+
if(root->left and (!root->left->left and !root->left->right)) {
28+
return root->left->val
29+
+ sumOfLeftLeaves(root->right);
30+
}
31+
return sumOfLeftLeaves(root->left)
32+
+ sumOfLeftLeaves(root->right);
33+
}
2034
};

source-code/Top_K_Frequent_Words.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ class Solution {
2121
if(pQ.size() < k) {
2222
pQ.push({entry->second, entry->first});
2323
} else {
24-
if(pQ.top().first < entry->second) {
25-
pQ.pop();
26-
pQ.push({entry->second, entry->first});
27-
} else if(pQ.top().first == entry->second and entry->first < pQ.top().second) {
24+
if((pQ.top().first < entry->second) or
25+
(pQ.top().first == entry->second and entry->first < pQ.top().second)) {
2826
pQ.pop();
2927
pQ.push({entry->second, entry->first});
3028
}

0 commit comments

Comments
 (0)