Skip to content

Commit 3b706d4

Browse files
committed
add some more questions and memo
1 parent 1b192f7 commit 3b706d4

11 files changed

+375
-149
lines changed

207 Course Schedule.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,25 @@
88
* @return {boolean}
99
*/
1010

11+
// http://www.cnblogs.com/Liok3187/p/4752700.html
12+
1113
var constructGraph = function(numNodes, pre) {
1214
var nodes = [];
1315
for (var i = 0; i < numNodes; i++) {
1416
var node = {};
1517
node.neighbors = [];
16-
nodes[i] = node;
18+
nodes.push(node);
1719
}
1820
for (var j = 0; j < pre.length; j++) {
19-
var s = pre[j][1];
20-
var d = pre[j][0];
21-
nodes[s].neighbors.push(nodes[d]);
21+
var requiredCourse = pre[j][1];
22+
var course = pre[j][0];
23+
// pushing course that require required-course to it's neighbor
24+
// when we go to the required-course, and traverse it's neighbors, we want to make sure that those neighbor doesn't have others that nodes
25+
// that required those neighbor plus those neighbor's required-course
26+
// example [1,0], [0,2], [2,1]
27+
// 1 required 0, 0 required 2, and 2 required 1
28+
// it creates loop
29+
nodes[requiredCourse].neighbors.push(nodes[course]);
2230
}
2331
return nodes;
2432
}
@@ -42,9 +50,13 @@ var canFinish = function(numCourses, prerequisites) {
4250
var nodes = constructGraph(numCourses, prerequisites);
4351

4452
for (var i = 0; i < nodes.length; i++) {
45-
console.log('nodes i',nodes[i])
46-
var hasCycle = dfs(nodes[i], []);
53+
var parent = [];
54+
var hasCycle = dfs(nodes[i], parent);
55+
56+
console.log(hasCycle, i, nodes[i], parent)
4757
if (hasCycle) return false;
4858
}
4959
return true;
50-
};
60+
};
61+
62+
canFinish(5, [[0,1],[1,2],[1,3],[1,4],[2,3]])
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def findKthLargest(self, nums, k):
3+
"""
4+
:type nums: List[int]
5+
:type k: int
6+
:rtype: int
7+
"""
8+
9+
pivot = random.choice(nums);
10+
nums1, nums2 = [], []
11+
for num in nums:
12+
if num > pivot:
13+
nums1.append(num)
14+
elif num < pivot:
15+
nums2.append(num)
16+
17+
if k <= len(nums1):
18+
return self.findKthLargest(nums1, k)
19+
if k > len(nums) - len(nums2): # draw a graph to visualize it! It's not in the top k assortment, but in the small section
20+
return self.findKthLargest(nums2, k - (len(nums) - len(nums2)))
21+
22+
return pivot

289. Game of Life.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @param {number[][]} board
3+
* @return {void} Do not return anything, modify board in-place instead.
4+
*/
5+
var gameOfLife = function(board) {
6+
// 3 over-population or under population
7+
// 2 means reproduction
8+
// 1 is alive
9+
// 0 is dead
10+
// if mod 2 === 1 means the current state is alive else dead
11+
12+
13+
var rows = board.length;
14+
var cols = board[0].length;
15+
16+
for(var i = 0; i < rows; i++) {
17+
for(var j = 0; j < cols; j++) {
18+
var cur = board[i][j];
19+
var count = 0;
20+
21+
if(i - 1 >= 0 && j -1 >= 0 && (board[i - 1][j - 1])%2 === 1) {
22+
count++;
23+
}
24+
if(i - 1 >= 0 && (board[i - 1][j])%2 === 1) {
25+
count++;
26+
}
27+
if(i - 1 >= 0 && j + 1 < cols && (board[i - 1][j + 1])%2 === 1) {
28+
count++;
29+
}
30+
if(j - 1 >= 0 && (board[i][j - 1])%2 === 1) {
31+
count++;
32+
}
33+
if(j + 1 < cols && (board[i][j + 1])%2 === 1) {
34+
count++;
35+
}
36+
if(i + 1 < rows && j - 1 >= 0 && (board[i + 1][j - 1])%2 === 1) {
37+
count++;
38+
}
39+
if(i + 1 < rows && (board[i + 1][j])%2 === 1) {
40+
count++;
41+
}
42+
if(i + 1 < rows && j + 1 < cols && (board[i + 1][j + 1])%2 === 1) {
43+
count++;
44+
}
45+
46+
if(board[i][j] === 1) {
47+
if(count > 3 || count < 2) {
48+
board[i][j] = 3;
49+
}
50+
} else {
51+
if(count === 3) {
52+
board[i][j] = 2;
53+
}
54+
}
55+
}
56+
}
57+
58+
for(i = 0; i < rows; i++) {
59+
for(j = 0; j < cols; j++) {
60+
if(board[i][j] === 3) {
61+
board[i][j] = 0;
62+
} else if(board[i][j] === 2) {
63+
board[i][j] = 1;
64+
}
65+
}
66+
}
67+
};

300 Longest Increasing Subsequence.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var lengthOfLIS = function(nums) {
6+
var size = nums.length;
7+
8+
if(size === 0) {
9+
return 0;
10+
}
11+
12+
var dp = Array.from({length: size}, () => 1);
13+
14+
for(var i = 0; i < size; i++) {
15+
for(var j = 0; j < i; j++) {
16+
if(nums[i] > nums[j]) {
17+
dp[i] = Math.max(dp[i], dp[j] + 1);
18+
}
19+
}
20+
}
21+
22+
return Math.max.apply(null, dp);
23+
};

335 Self Crossing.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} x
3+
* @return {boolean}
4+
*/
5+
6+
// http://www.cnblogs.com/grandyang/p/5216856.html
7+
var isSelfCrossing = function(x) {
8+
9+
for(var i = 3; i < x.length; i++) {
10+
if (x[i] >= x[i - 2] && x[i - 3] >= x[i - 1]) {
11+
return true;
12+
}
13+
if(i >= 4 && x[i-1] == x[i-3] && x[i] >= (x[i-2] - x[i-4])) {
14+
return true;
15+
}
16+
if(i >= 5 && x[i-2] >= x[i-4] && x[i-3] >= x[i-1] && x[i-1] >= (x[i-3] - x[i-5]) && x[i] >= (x[i-2] - x[i-4])) {
17+
return true;
18+
}
19+
}
20+
21+
return false;
22+
};

37 Sudoku Solver.js

Whitespace-only changes.

4. Median of Two Sorted Arrays.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @param {number[]} nums1
3+
* @param {number[]} nums2
4+
* @return {number}
5+
*/
6+
7+
// http://blog.csdn.net/yutianzuijin/article/details/11499917
8+
var findMedianSortedArrays = function(nums1, nums2) {
9+
var m = nums1.length;
10+
var n = nums2.length;
11+
var total = m + n;
12+
13+
if(total%2 === 1) {
14+
return findKth(nums1, m, nums2, n, parseInt(total/2) + 1);
15+
} else {
16+
return (findKth(nums1, m, nums2, n, parseInt(total/2)) + findKth(nums1, m, nums2, n, parseInt(total/2) + 1))/2;
17+
}
18+
};
19+
20+
21+
function findKth(a, m, b, n, k) {
22+
// always assume that m is equal or smaller than n
23+
if(m > n) {
24+
return findKth(b, n, a, m, k);
25+
}
26+
27+
if(m === 0) {
28+
return b[k-1];
29+
}
30+
31+
if(k === 1) {
32+
return Math.min(a[0],b[0]);
33+
}
34+
35+
// divide k into two parts
36+
var pa = Math.min(parseInt(k/2), m);
37+
var pb = k - pa;
38+
39+
if(a[pa - 1] < b[pb - 1]) {
40+
return findKth(a.slice(pa), m - pa, b, n, k - pa);
41+
} else if(a[pa - 1] > b[pb - 1]) {
42+
return findKth(a, m, b.slice(pb), n - pb, k - pb);
43+
} else {
44+
return a[pa - 1];
45+
}
46+
}

0 commit comments

Comments
 (0)