Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5f09c4d

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

13 files changed

+689
-657
lines changed

‎README.md

Lines changed: 594 additions & 593 deletions
Large diffs are not rendered by default.

‎source-code/Add_Two_Numbers_II.cpp

Lines changed: 2 additions & 5 deletions
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;
Lines changed: 35 additions & 0 deletions
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

Lines changed: 1 addition & 7 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 22 additions & 23 deletions
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 = newChain();
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 12 additions & 17 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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) -> boolconst { 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});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /