Skip to content

Commit 3121d3d

Browse files
committed
add and modifed some solutions
1 parent dbf7b50 commit 3121d3d

22 files changed

+687
-54
lines changed

127 Word Ladder.js

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,77 @@ var ladderLength = function(beginWord, endWord, wordList) {
6565
}
6666

6767
return 0;
68-
};
68+
};
69+
70+
// will time exceeded. javascript hash is slower than set
71+
var ladderLength = function(beginWord, endWord, wordList) {
72+
if(beginWord === endWord) {
73+
return 0;
74+
}
75+
76+
var queue = [];
77+
var visited = {};
78+
var count = 1;
79+
var baseCharCode = 'a'.charCodeAt(0);
80+
81+
queue.push(beginWord);
82+
83+
while(queue.length) {
84+
var len = queue.length;
85+
86+
for(var i = 0; i < len; i++) {
87+
var word = queue.shift();
88+
89+
for(var j = 0; j < word.length; j++) {
90+
for(var k = 0; k < 26; k++) {
91+
var newChar = String.fromCharCode(baseCharCode + k);
92+
var newWord = word.substring(0, j) + newChar + word.substring(j + 1);
93+
94+
if(newWord === endWord) {
95+
return count + 1;
96+
}
97+
98+
if(!visited[newWord] && wordList.has(newWord)) {
99+
visited[newWord] = true;
100+
queue.push(newWord);
101+
}
102+
}
103+
}
104+
}
105+
106+
count++;
107+
}
108+
109+
return 0;
110+
};
111+
112+
113+
114+
115+
Hi Thiago
116+
117+
I very much appreciate that you took the time writing this warm welcoming letter and provided me the opportunity to come onsite visiting the team at Periscope.
118+
After much thought, I've decided to accept offer at another company. It was really a tough call for me since I really like the product, role and people I met during my visit.
119+
Again, I cannot thank you enough for your time, and support. It's been a great pleasure to know you and the team. I hope that we cross paths in the near future.
120+
121+
Wish you, teams, and Periscope all the success.
122+
123+
Regards,
124+
Jerry
125+
126+
127+
128+
129+
Hi Cynthia
130+
131+
Thank your for patience and support along the way.
132+
I very much appreciate that you took the time answering many of my questions about the Periscope, and role.
133+
134+
After much thought, I've decided to accept offer at another company. It was really a tough call for me since I really like the product and people I met during my visit.
135+
136+
Again, I cannot thank you enough for your time, and support. It's been a great pleasure to know you and the team. I hope that we cross paths in the near future.
137+
138+
Wish you, teams, and Periscope all the success.
139+
140+
Regards,
141+
Jerry

128 Longest Consecutive Sequence.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
2+
3+
// For example,
4+
// Given [100, 4, 200, 1, 3, 2],
5+
// The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
6+
7+
// Your algorithm should run in O(n) complexity.
8+
9+
// Hide Company Tags Google Facebook
10+
// Hide Tags Array Union Find
11+
// Hide Similar Problems (M) Binary Tree Longest Consecutive Sequence
12+
13+
14+
/**
15+
* @param {number[]} nums
16+
* @return {number}
17+
*/
18+
var longestConsecutive = function(nums) {
19+
var maxLen = -Infinity;
20+
var hash = {};
21+
22+
for(var i = 0; i < nums.length; i++) {
23+
hash[nums[i]] = 1;
24+
}
25+
26+
var visited = {};
27+
28+
for(i = 0; i < nums.length; i++) {
29+
var val = nums[i];
30+
if(visited[val]) {
31+
continue;
32+
}
33+
visited[val] = true;
34+
var len = 1;
35+
var preVal = val - 1;
36+
while(hash[preVal]) {
37+
len++
38+
visited[preVal--] = true;
39+
}
40+
var nxtVal = val + 1;
41+
while(hash[nxtVal]) {
42+
len++
43+
visited[nxtVal++] = true;
44+
}
45+
46+
if(len > maxLen) {
47+
maxLen = len;
48+
}
49+
}
50+
51+
return maxLen;
52+
};

146 LRU Cache.js

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,116 @@ LRUCache.prototype.set = function(key, value) {
9191
}
9292

9393
this.map.set(key, newNode);
94-
};
94+
};
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
// Second Implementation
106+
107+
108+
function DoublyLinkListNode(key, value) {
109+
this.key = key;
110+
this.value = value;
111+
this.prev = this.next = null;
112+
}
113+
114+
/**
115+
* @constructor
116+
*/
117+
var LRUCache = function(capacity) {
118+
this.head = this.tail = null;
119+
this.maxCapacity = capacity;
120+
this.currSize = 0;
121+
this.hash = {};
122+
};
123+
124+
/**
125+
* @param {number} key
126+
* @returns {number}
127+
*/
128+
LRUCache.prototype.get = function(key) {
129+
if(!this.hash[key]) {
130+
return -1;
131+
}
132+
133+
this.moveToHead(key);
134+
return this.hash[key].value;
135+
};
136+
137+
/**
138+
* @param {number} key
139+
* @param {number} value
140+
* @returns {void}
141+
*/
142+
LRUCache.prototype.set = function(key, value) {
143+
if(this.maxCapacity <= 0) {
144+
return;
145+
}
146+
147+
if(!this.hash[key]) {
148+
149+
if(this.currSize === this.maxCapacity) {
150+
this.removeLast();
151+
this.currSize--;
152+
}
153+
154+
this.hash[key] = new DoublyLinkListNode(key, value);
155+
this.currSize++;
156+
}
157+
158+
this.hash[key].value = value;
159+
this.moveToHead(key);
160+
};
161+
162+
LRUCache.prototype.removeLast = function() {
163+
if(this.tail === null) {
164+
return;
165+
}
166+
167+
delete this.hash[this.tail.key];
168+
var newTail = this.tail.prev;
169+
170+
if(newTail === null) {
171+
this.head = this.tail = null;
172+
return;
173+
}
174+
175+
this.tail.prev = null;
176+
newTail.next = null;
177+
this.tail = newTail;
178+
}
179+
180+
LRUCache.prototype.moveToHead = function(key) {
181+
var newHead = this.hash[key];
182+
183+
if(this.head === null && this.tail === null) {
184+
this.head = this.tail = newHead;
185+
}
186+
187+
if(newHead === this.head) {
188+
return;
189+
}
190+
191+
if(newHead === this.tail) {
192+
this.tail = newHead.prev;
193+
}
194+
195+
if(newHead.prev) {
196+
newHead.prev.next = newHead.next;
197+
}
198+
if(newHead.next) {
199+
newHead.next.prev = newHead.prev;
200+
}
201+
202+
newHead.prev = null;
203+
newHead.next = this.head;
204+
this.head.prev = newHead;
205+
this.head = newHead;
206+
}

157 Read N Characters Given Read4.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,7 @@ var solution = function(read4) {
5454

5555
return total;
5656
};
57-
};
57+
};
58+
59+
60+
// [tricky] [important]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// The API: int read4(char *buf) reads 4 characters at a time from a file.
2+
3+
// The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.
4+
5+
// By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.
6+
7+
// Note:
8+
// The read function may be called multiple times.
9+
10+
// Hide Company Tags Bloomberg Google Facebook
11+
// Hide Tags String
12+
// Hide Similar Problems (E) Read N Characters Given Read4
13+
14+
15+
16+
/**
17+
* Definition for read4()
18+
*
19+
* @param {character[]} buf Destination buffer
20+
* @return {number} The number of characters read
21+
* read4 = function(buf) {
22+
* ...
23+
* };
24+
*/
25+
26+
/**
27+
* @param {function} read4()
28+
* @return {function}
29+
*/
30+
var solution = function(read4) {
31+
let bufRead = [];
32+
let count = 0; // how many characters read with read4
33+
let i = 0;
34+
35+
/**
36+
* @param {character[]} buf Destination buffer
37+
* @param {number} n Maximum number of characters to read
38+
* @return {number} The number of characters read
39+
*/
40+
return function(buf, n) {
41+
let numChrRead = 0;
42+
43+
while (numChrRead < n) {
44+
if (i === 0) {
45+
count = read4(bufRead);
46+
}
47+
48+
while (i < count && numChrRead < n) {
49+
buf[numChrRead++] = bufRead[i++];
50+
}
51+
52+
// read4 buffer used up, start over
53+
if (i === count) {
54+
i = 0;
55+
}
56+
57+
// end of file
58+
if (count < 4) {
59+
break;
60+
}
61+
}
62+
63+
return numChrRead;
64+
};
65+
};

20 Valid Parentheses.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,26 @@ var isValid = function(s) {
2727
index++;
2828
}
2929

30+
return stack.length === 0;
31+
};
32+
33+
// second attempt
34+
35+
var isValid = function(s) {
36+
var stack = [];
37+
38+
for(var i = 0; i < s.length; i++) {
39+
var chr = s[i];
40+
41+
if(chr === '(' || chr === '{' || chr === '[') {
42+
stack.push(chr);
43+
} else if(chr === ')' || chr === '}' || chr === ']') {
44+
var top = stack.pop();
45+
if(!top || (top === '(' && chr !== ')') || (top === '{' && chr !== '}') || (top === '[' && chr !== ']')) {
46+
return false;
47+
}
48+
}
49+
}
50+
3051
return stack.length === 0;
3152
};

0 commit comments

Comments
 (0)