Skip to content

Commit bc11182

Browse files
Merge branch 'master' into parent
2 parents 11225d7 + cbb3965 commit bc11182

File tree

6 files changed

+467
-1
lines changed

6 files changed

+467
-1
lines changed

CoinChange.js

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
3+
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/description/
4+
5+
862. Shortest Subarray with Sum at Least K
6+
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
7+
8+
If there is no non-empty subarray with sum at least K, return -1.
9+
10+
11+
12+
Example 1:
13+
14+
Input: A = [1], K = 1
15+
Output: 1
16+
Example 2:
17+
18+
Input: A = [1,2], K = 4
19+
Output: -1
20+
Example 3:
21+
22+
Input: A = [2,-1,2], K = 3
23+
Output: 3
24+
25+
26+
Note:
27+
28+
1 <= A.length <= 50000
29+
-10 ^ 5 <= A[i] <= 10 ^ 5
30+
1 <= K <= 10 ^ 9
31+
*/
32+
33+
34+
/*
35+
Tree for solution
36+
37+
38+
[pos, countOfNumbers, K]
39+
[pos + 1, countOfNumbers, K] [pos + 1, countOfNumbers + 1, K - nums[pos]] [pos, countOfNumbers + 1, K - nums[pos]]
40+
...
41+
42+
[0, 0, 11] // [pos, countOfNumbers, K]
43+
/ | \
44+
[1, 0, 11] [1, 1, 10] [0, 1, 10]
45+
/ | \ / | \ / | \
46+
[2, 0, 11] [2, 1, 9] [1, 1, 9] [2, 0, 10] [2, 2, 8] [1, 2, 8] [1, 1, 10] [1, 2, 9] [0, 2, 9]
47+
...
48+
*/
49+
50+
// Solution 3
51+
var coinChange3 = function(coins, amount) {
52+
var memo = [];
53+
54+
for(var i = 0; i <= amount; i++) {
55+
memo[i] = Number.POSITIVE_INFINITY;
56+
}
57+
memo[0] = 0;
58+
59+
for(var i = 0; i < coins.length; i++) {
60+
const coin = coins[i];
61+
for(var j = coin; j < memo.length; j++) {
62+
memo[j] = min2(memo[j], memo[j - coin] + 1);
63+
}
64+
}
65+
66+
return (memo[amount] == Number.POSITIVE_INFINITY) ? -1 : memo[amount];
67+
};
68+
69+
var min2 = function(a, b) {
70+
return (a < b) ? a : b;
71+
}
72+
73+
74+
// Solution 2
75+
var buildMemoKey = function(position, amount) {
76+
return position + "-" + amount;
77+
}
78+
79+
var coinChange2 = function(coins, amount) {
80+
var memo = {};
81+
var solution = coinChangeAux2(coins, amount, 0, memo, Number.POSITIVE_INFINITY);
82+
return solution == Number.POSITIVE_INFINITY ? -1 : solution;
83+
};
84+
85+
var coinChangeAux2 = function(coins, amount, pos, memo) {
86+
var key = buildMemoKey(pos, amount);
87+
if(memo[key]) {
88+
return memo[key];
89+
}
90+
if(amount < 0) {
91+
return Number.POSITIVE_INFINITY;
92+
} else if(amount == 0) {
93+
return 0;
94+
} else if(pos >= coins.length) {
95+
return Number.POSITIVE_INFINITY;
96+
}
97+
98+
var left = coinChangeAux2(coins, amount, pos + 1, memo);
99+
var middle = 1 + coinChangeAux2(coins, amount - coins[pos], pos + 1, memo);
100+
var right = 1 + coinChangeAux2(coins, amount - coins[pos], pos, memo);
101+
102+
var solution = min(left, middle, right);
103+
memo[key] = solution;
104+
return solution;
105+
}
106+
107+
// Solution 1 naive
108+
var coinChange1 = function(coins, amount) {
109+
var solution = coinChangeAux1(coins, amount, 0);
110+
return solution == Number.POSITIVE_INFINITY ? -1 : solution;
111+
};
112+
113+
var coinChangeAux1 = function(coins, amount, pos) {
114+
if(amount < 0) {
115+
return Number.POSITIVE_INFINITY;
116+
} else if(amount == 0) {
117+
return 0;
118+
} else if(pos >= coins.length) {
119+
return Number.POSITIVE_INFINITY;
120+
}
121+
122+
var left = coinChangeAux1(coins, amount, pos + 1);
123+
var middle = 1 + coinChangeAux1(coins, amount - coins[pos], pos + 1);
124+
var right = 1 + coinChangeAux1(coins, amount - coins[pos], pos);
125+
126+
var partialSol = min(left, middle, right);
127+
return partialSol;
128+
}
129+
130+
var min = function(a, b, c) {
131+
if(a < b) { return (a < c) ? a : c };
132+
return (b < c) ? b : c;
133+
}
134+
135+
136+
function main() {
137+
console.log("-------------");
138+
console.log("Approach 1")
139+
console.log(coinChange1([], 3));
140+
console.log(coinChange1([2], 3));
141+
console.log(coinChange1([1, 2, 5], 11));
142+
console.log(coinChange1([3,7,405,436], 8839));
143+
// console.log(coinChange1([370,417,408,156,143,434,168,83,177,280,117], 9953)); takes forever
144+
145+
console.log("-------------");
146+
console.log("Approach 2")
147+
console.log(coinChange2([], 3));
148+
console.log(coinChange2([2], 3));
149+
console.log(coinChange2([1, 2, 5], 11));
150+
console.log(coinChange2([3,7,405,436], 8839));
151+
console.log(coinChange2([370,417,408,156,143,434,168,83,177,280,117], 9953));
152+
153+
console.log("-------------");
154+
console.log("Approach 3")
155+
console.log(coinChange3([], 3));
156+
console.log(coinChange3([2], 3));
157+
console.log(coinChange3([1, 2, 5], 11));
158+
console.log(coinChange3([3,7,405,436], 8839));
159+
console.log(coinChange3([370,417,408,156,143,434,168,83,177,280,117], 9953));
160+
}
161+
main();

FloodFill.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
https://leetcode.com/problems/flood-fill/description/
3+
4+
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).
5+
6+
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
7+
8+
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
9+
10+
At the end, return the modified image.
11+
12+
Example 1:
13+
Input:
14+
image = [[1,1,1],[1,1,0],[1,0,1]]
15+
sr = 1, sc = 1, newColor = 2
16+
Output: [[2,2,2],[2,2,0],[2,0,1]]
17+
Explanation:
18+
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
19+
by a path of the same color as the starting pixel are colored with the new color.
20+
Note the bottom corner is not colored 2, because it is not 4-directionally connected
21+
to the starting pixel.
22+
Note:
23+
24+
The length of image and image[0] will be in the range [1, 50].
25+
The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
26+
The value of each color in image[i][j] and newColor will be an integer in [0, 65535].
27+
28+
*/
29+
30+
var floodFill = function(image, sr, sc, newColor) {
31+
var oldColor = image[sr][sc];
32+
33+
if(newColor == oldColor) {
34+
return image;
35+
}
36+
37+
image[sr][sc] = newColor;
38+
39+
if(sr > 0 && image[sr - 1][sc] == oldColor) {
40+
floodFill(image, sr - 1, sc, newColor); //Left
41+
}
42+
if(sc > 0 && image[sr][sc - 1] == oldColor) {
43+
floodFill(image, sr, sc - 1, newColor); //Up
44+
}
45+
if(sr < image.length - 1 && image[sr + 1][sc] == oldColor) {
46+
floodFill(image, sr + 1, sc, newColor); //Down
47+
}
48+
if(sc < image[0].length - 1 && image[sr][sc + 1] == oldColor) {
49+
floodFill(image, sr, sc + 1, newColor); // Right
50+
}
51+
return image;
52+
};
53+
54+
55+
function main() {
56+
console.log(floodFill([[1,1,1],[1,1,0],[1,0,1]], 1, 1, 2))
57+
}
58+
59+
module.exports.main = main;

Main.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@ var permutationWithoutDuplicates = require("./PermutationsWithoutDuplicates.js")
33
var permutationWithDuplicates = require("./PermutationsWithDuplicates.js");
44
var subsets = require("./Subsets.js");
55
var generateParentheses = require("./GenerateParentheses.js")
6+
var maximunSubarray = require("./MaximunSubarray.js");
7+
var coinChange = require("./CoinChange.js");
8+
var nQueens = require("./NQueens.js");
9+
var uniquePaths = require("./UniquePaths.js");
10+
var floodFill = require("./FloodFill.js")
611

712
// Invocation
813

914
// permutationWithoutDuplicates.main();
1015
// permutationWithDuplicates.main();
1116
// subsets.main();
12-
// generateParentheses.main();
17+
// generateParentheses.main();
18+
// maximunSubarray.main();
19+
// coinChange.main();
20+
// nQueens.main();
21+
// uniquePaths.main();
22+
// floodFill.main();

MaximunSubarray.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
https://leetcode.com/problems/maximum-subarray/submissions/1
3+
4+
. Maximum Subarray
5+
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
6+
7+
Example:
8+
9+
Input: [-2,1,-3,4,-1,2,1,-5,4],
10+
Output: 6
11+
Explanation: [4,-1,2,1] has the largest sum = 6.
12+
Follow up:
13+
14+
*/
15+
16+
var maxSubArray = function(nums) {
17+
if(nums.length == 0) { return 0 };
18+
var maxSub = nums[0];
19+
var currentMax = nums[0];
20+
21+
for(var i = 1; i < nums.length; i++) {
22+
currentMax = max(nums[i], currentMax + nums[i]);
23+
if(currentMax > maxSub) {
24+
maxSub = currentMax;
25+
}
26+
}
27+
28+
return maxSub;
29+
};
30+
31+
var max = function(i, j) {
32+
return (i > j) ? i : j;
33+
}
34+
35+
var main = function() {
36+
console.log(maxSubArray([4,1,-1,4,5,6,7,-200]));
37+
}
38+
39+
module.exports.main = main;

NQueens.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* https://leetcode.com/problems/n-queens/description/
2+
3+
Example:
4+
5+
Input: 4
6+
Output: [
7+
[".Q..", // Solution 1
8+
"...Q",
9+
"Q...",
10+
"..Q."],
11+
12+
["..Q.", // Solution 2
13+
"Q...",
14+
"...Q",
15+
".Q.."]
16+
]
17+
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
18+
*/
19+
20+
/**
21+
* @param {number} n
22+
* @return {string[][]}
23+
*/
24+
var solveNQueens = function(n) {
25+
var sol = [];
26+
solveNQueensAux(n, 0, new Set(), new Set(), new Set(), [], sol);
27+
return parseSolutions(sol, n);
28+
};
29+
30+
var solveNQueensAux = function(n, row, diagonalDiffs, diagonalSums, cols, currentSol, sol) {
31+
if(row == n) {
32+
sol.push(currentSol);
33+
return;
34+
}
35+
36+
for(var i = 0; i < n; i++) {
37+
const diagonalDiff = i - row;
38+
const diagonalSum = i + row;
39+
if(!diagonalDiffs.has(diagonalDiff) && !cols.has(i) && !diagonalSums.has(diagonalSum)) {
40+
diagonalDiffs.add(diagonalDiff);
41+
diagonalSums.add(diagonalSum);
42+
cols.add(i);
43+
solveNQueensAux(n, row + 1, diagonalDiffs, diagonalSums, cols, [...currentSol, ...[[row, i]]], sol);
44+
cols.delete(i);
45+
diagonalDiffs.delete(diagonalDiff);
46+
diagonalSums.delete(diagonalSum);
47+
}
48+
}
49+
return;
50+
}
51+
52+
var parseSolutions = function(sols, n) {
53+
var matrixes = [];
54+
for(var i = 0; i < sols.length; i++) {
55+
var sol = sols[i];
56+
var matrix = [];
57+
for(var row = 0; row < n; row++) {
58+
matrix[row] = []
59+
const queenPos = sol[row];
60+
for(var col = 0; col < n; col++) {
61+
matrix[row] += (queenPos[1] == col) ? "Q" : ".";
62+
}
63+
}
64+
65+
matrixes.push(matrix);
66+
}
67+
68+
return matrixes;
69+
}
70+
71+
var printMatrixes = function(matrixes, n) {
72+
console.log("Start solution of n: " + n);
73+
for(var i = 0; i < matrixes.length; i++) {
74+
printMatrix(matrixes[i]);
75+
}
76+
console.log("End solution of n: " + n);
77+
}
78+
79+
var printMatrix = function(matrix) {
80+
console.log("------------");
81+
for(var i = 0; i < matrix.length; i++) {
82+
console.log(matrix[i]);
83+
}
84+
console.log("------------");
85+
}
86+
87+
var main = function(n) {
88+
printMatrixes(solveNQueens(4), 4);
89+
printMatrixes(solveNQueens(5), 5);
90+
printMatrixes(solveNQueens(6), 6);
91+
}
92+
main();

0 commit comments

Comments
 (0)