-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dbf7b50
commit 3121d3d
Showing
22 changed files
with
687 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,4 +54,7 @@ var solution = function(read4) { | |
|
||
return total; | ||
}; | ||
}; | ||
}; | ||
|
||
|
||
// [tricky] [important] |
65 changes: 65 additions & 0 deletions
65
158 Read N Characters Give Read4 II - Call Multiple Times.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.