Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9fd3167

Browse files
Merge branch 'master' into n-queens
2 parents e0d1fdf + 0f71258 commit 9fd3167

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ var permutationWithoutDuplicates = require("./PermutationsWithoutDuplicates.js")
33
var permutationWithDuplicates = require("./PermutationsWithDuplicates.js");
44
var subsets = require("./Subsets.js");
55
var nQueens = require("./NQueens.js");
6+
var uniquePaths = require("./UniquePaths.js");
7+
var floodFill = require("./FloodFill.js")
68

79
// Invocation
810

911
// permutationWithoutDuplicates.main();
1012
// permutationWithDuplicates.main();
1113
// subsets.main();
1214
// nQueens.main();
15+
// uniquePaths.main();
16+
// floodFill.main();

UniquePaths.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
3+
62. Unique Paths
4+
https://leetcode.com/problems/unique-paths/description/
5+
6+
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
7+
8+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
9+
10+
How many possible unique paths are there?
11+
12+
Example 1:
13+
14+
Input: m = 3, n = 2
15+
Output: 3
16+
Explanation:
17+
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
18+
1. Right -> Right -> Down
19+
2. Right -> Down -> Right
20+
3. Down -> Right -> Right
21+
Example 2:
22+
23+
Input: m = 7, n = 3
24+
Output: 28
25+
26+
*/
27+
28+
// Solution 1
29+
// This solution is a naive solution implementing a binary tree and visiting each node.
30+
31+
var uniquePaths1 = function(m, n) {
32+
return uniquePathsAux(0, 0, m, n)
33+
};
34+
35+
var uniquePathsAux = function(row, col, rowLength, colLength) {
36+
if(row >= rowLength || col >= colLength) {
37+
return 0;
38+
}
39+
if(row == rowLength - 1 && col == colLength - 1) {
40+
return 1;
41+
}
42+
43+
return uniquePathsAux(row + 1, col, rowLength, colLength) +
44+
uniquePathsAux(row, col + 1, rowLength, colLength)
45+
};
46+
47+
// Solution 2
48+
// This solution is solution 1 but memoized.
49+
var uniquePaths2 = function(m, n) {
50+
var memo = {};
51+
return uniquePathsAux2(0, 0, m, n, memo)
52+
};
53+
54+
var uniquePathsAux2 = function(row, col, rowLength, colLength, memo) {
55+
if(memo[memoKey(row, col)]) {
56+
return memo[row + "-" + col];
57+
}
58+
if(row >= rowLength || col >= colLength) {
59+
return 0;
60+
}
61+
if(row == rowLength - 1 && col == colLength - 1) {
62+
return 1;
63+
}
64+
65+
var result = uniquePathsAux(row + 1, col, rowLength, colLength, memo) +
66+
uniquePathsAux(row, col + 1, rowLength, colLength, memo);
67+
memo[memoKey(row, col)] = result;
68+
return result;
69+
};
70+
71+
var memoKey = function(row, col) {
72+
return row + "-" + col;
73+
}
74+
75+
// Solution 3
76+
// This solution uses Dinamic Programming
77+
var uniquePaths3 = function(m, n) {
78+
var matrix = [];
79+
for(var i = 0; i < m; i++) {
80+
matrix[i] = [];
81+
for(var j = 0; j < n; j++) {
82+
if(i == 0 || j == 0) {
83+
matrix[i][j] = 1;
84+
} else{
85+
matrix[i][j] = 0;
86+
}
87+
}
88+
}
89+
90+
for(var row = 1; row < m; row++) {
91+
for(var col = 1; col < n; col++) {
92+
matrix[row][col] = matrix[row - 1][col] + matrix[row][col - 1]
93+
}
94+
}
95+
96+
return matrix[m - 1][n - 1];
97+
};
98+
99+
var main = function() {
100+
console.log(uniquePaths1(10,4));
101+
console.log(uniquePaths2(10,4));
102+
console.log(uniquePaths3(10,4));
103+
}
104+
105+
module.exports.main = main;

0 commit comments

Comments
 (0)