Skip to content

Commit

Permalink
add and modifed some solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
chihungyu1116 committed Aug 28, 2016
1 parent dbf7b50 commit 3121d3d
Show file tree
Hide file tree
Showing 22 changed files with 687 additions and 54 deletions.
75 changes: 74 additions & 1 deletion 127 Word Ladder.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,77 @@ var ladderLength = function(beginWord, endWord, wordList) {
}

return 0;
};
};

// will time exceeded. javascript hash is slower than set
var ladderLength = function(beginWord, endWord, wordList) {
if(beginWord === endWord) {
return 0;
}

var queue = [];
var visited = {};
var count = 1;
var baseCharCode = 'a'.charCodeAt(0);

queue.push(beginWord);

while(queue.length) {
var len = queue.length;

for(var i = 0; i < len; i++) {
var word = queue.shift();

for(var j = 0; j < word.length; j++) {
for(var k = 0; k < 26; k++) {
var newChar = String.fromCharCode(baseCharCode + k);
var newWord = word.substring(0, j) + newChar + word.substring(j + 1);

if(newWord === endWord) {
return count + 1;
}

if(!visited[newWord] && wordList.has(newWord)) {
visited[newWord] = true;
queue.push(newWord);
}
}
}
}

count++;
}

return 0;
};




Hi Thiago

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.
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.
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.

Wish you, teams, and Periscope all the success.

Regards,
Jerry




Hi Cynthia

Thank your for patience and support along the way.
I very much appreciate that you took the time answering many of my questions about the Periscope, and role.

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.
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.

Wish you, teams, and Periscope all the success.

Regards,
Jerry
52 changes: 52 additions & 0 deletions 128 Longest Consecutive Sequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

// For example,
// Given [100, 4, 200, 1, 3, 2],
// The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

// Your algorithm should run in O(n) complexity.

// Hide Company Tags Google Facebook
// Hide Tags Array Union Find
// Hide Similar Problems (M) Binary Tree Longest Consecutive Sequence


/**
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function(nums) {
var maxLen = -Infinity;
var hash = {};

for(var i = 0; i < nums.length; i++) {
hash[nums[i]] = 1;
}

var visited = {};

for(i = 0; i < nums.length; i++) {
var val = nums[i];
if(visited[val]) {
continue;
}
visited[val] = true;
var len = 1;
var preVal = val - 1;
while(hash[preVal]) {
len++
visited[preVal--] = true;
}
var nxtVal = val + 1;
while(hash[nxtVal]) {
len++
visited[nxtVal++] = true;
}

if(len > maxLen) {
maxLen = len;
}
}

return maxLen;
};
114 changes: 113 additions & 1 deletion 146 LRU Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,116 @@ LRUCache.prototype.set = function(key, value) {
}

this.map.set(key, newNode);
};
};










// Second Implementation


function DoublyLinkListNode(key, value) {
this.key = key;
this.value = value;
this.prev = this.next = null;
}

/**
* @constructor
*/
var LRUCache = function(capacity) {
this.head = this.tail = null;
this.maxCapacity = capacity;
this.currSize = 0;
this.hash = {};
};

/**
* @param {number} key
* @returns {number}
*/
LRUCache.prototype.get = function(key) {
if(!this.hash[key]) {
return -1;
}

this.moveToHead(key);
return this.hash[key].value;
};

/**
* @param {number} key
* @param {number} value
* @returns {void}
*/
LRUCache.prototype.set = function(key, value) {
if(this.maxCapacity <= 0) {
return;
}

if(!this.hash[key]) {

if(this.currSize === this.maxCapacity) {
this.removeLast();
this.currSize--;
}

this.hash[key] = new DoublyLinkListNode(key, value);
this.currSize++;
}

this.hash[key].value = value;
this.moveToHead(key);
};

LRUCache.prototype.removeLast = function() {
if(this.tail === null) {
return;
}

delete this.hash[this.tail.key];
var newTail = this.tail.prev;

if(newTail === null) {
this.head = this.tail = null;
return;
}

this.tail.prev = null;
newTail.next = null;
this.tail = newTail;
}

LRUCache.prototype.moveToHead = function(key) {
var newHead = this.hash[key];

if(this.head === null && this.tail === null) {
this.head = this.tail = newHead;
}

if(newHead === this.head) {
return;
}

if(newHead === this.tail) {
this.tail = newHead.prev;
}

if(newHead.prev) {
newHead.prev.next = newHead.next;
}
if(newHead.next) {
newHead.next.prev = newHead.prev;
}

newHead.prev = null;
newHead.next = this.head;
this.head.prev = newHead;
this.head = newHead;
}
5 changes: 4 additions & 1 deletion 157 Read N Characters Given Read4.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ var solution = function(read4) {

return total;
};
};
};


// [tricky] [important]
65 changes: 65 additions & 0 deletions 158 Read N Characters Give Read4 II - Call Multiple Times.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// The API: int read4(char *buf) reads 4 characters at a time from a file.

// 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.

// By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.

// Note:
// The read function may be called multiple times.

// Hide Company Tags Bloomberg Google Facebook
// Hide Tags String
// Hide Similar Problems (E) Read N Characters Given Read4



/**
* Definition for read4()
*
* @param {character[]} buf Destination buffer
* @return {number} The number of characters read
* read4 = function(buf) {
* ...
* };
*/

/**
* @param {function} read4()
* @return {function}
*/
var solution = function(read4) {
let bufRead = [];
let count = 0; // how many characters read with read4
let i = 0;

/**
* @param {character[]} buf Destination buffer
* @param {number} n Maximum number of characters to read
* @return {number} The number of characters read
*/
return function(buf, n) {
let numChrRead = 0;

while (numChrRead < n) {
if (i === 0) {
count = read4(bufRead);
}

while (i < count && numChrRead < n) {
buf[numChrRead++] = bufRead[i++];
}

// read4 buffer used up, start over
if (i === count) {
i = 0;
}

// end of file
if (count < 4) {
break;
}
}

return numChrRead;
};
};
21 changes: 21 additions & 0 deletions 20 Valid Parentheses.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,26 @@ var isValid = function(s) {
index++;
}

return stack.length === 0;
};

// second attempt

var isValid = function(s) {
var stack = [];

for(var i = 0; i < s.length; i++) {
var chr = s[i];

if(chr === '(' || chr === '{' || chr === '[') {
stack.push(chr);
} else if(chr === ')' || chr === '}' || chr === ']') {
var top = stack.pop();
if(!top || (top === '(' && chr !== ')') || (top === '{' && chr !== '}') || (top === '[' && chr !== ']')) {
return false;
}
}
}

return stack.length === 0;
};
Loading

0 comments on commit 3121d3d

Please sign in to comment.