Skip to content

Commit ef13e43

Browse files
committed
733_Flood_Fill
1 parent b6ea024 commit ef13e43

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ Also, there are open source implementations for basic data structs and algorithm
161161
| 716 | [Max Stack](https://leetcode.com/problems/max-stack/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/716_Max_Stack.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/716_Max_Stack.java) | 1. Two stacks<br> 2. Double linked list and Hash |
162162
| 720 | [Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/720_Longest_Word_in_Dictionary.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/720_Longest_Word_in_Dictionary.java) | 1. Brute Force, O(sum(w^2)) and O(w)<br>2. Tire Tree, O(sum(w) and O(w))<br>3. Sort and word without last char, O(nlogn + sum(w)) and O(w) |
163163
| 724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/724_Find_Pivot_Index.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/724_Find_Pivot_Index.java) | Seach the array to find a place where left sum is equal to right sum, O(n) and O(1) |
164+
| 733 | [Flood Fill](https://leetcode.com/problems/flood-fill/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/733_Flood_Fill.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/733_Flood_Fill.java) | 1. DFS with stack or recursive, O(n) and O(n)<br>2. BFS with queue, O(n) and O(n) |
164165
| 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/743_Network_Delay_Time.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/743_Network_Delay_Time.java) | Let V == N, then: 1. DFS, O(V^V+ElgE), O(V+E)<br>2. Dijkstra, O(V^2+E), O(V+E)|
165166
| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/771_Jewels_and_Stones.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/771_Jewels_and_Stones.java) | Count given char in string. Hash or table. [Oneline](https://leetcode.com/problems/jewels-and-stones/discuss/113574/1-liners-PythonJavaRuby) |
166167
| 804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/804_Unique_Morse_Code_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/804_Unique_Morse_Code_Words.java) | String, Hash and Set. Set is recommended. |

java/733_Flood_Fill.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
/*public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
3+
if (image[sr][sc] == newColor) return image;
4+
dfs(image, sr, sc, image[sr][sc], newColor);
5+
return image;
6+
}
7+
8+
private void dfs(int[][] image, int r, int c, int color, int newColor) {
9+
// Recursively DFS
10+
if (image[r][c] == color) {
11+
image[r][c] = newColor;
12+
if (r - 1 >= 0) dfs(image, r - 1, c, color, newColor);
13+
if (r + 1 < image.length) dfs(image, r + 1, c, color, newColor);
14+
if (c - 1 >= 0) dfs(image, r, c - 1, color, newColor);
15+
if (c + 1 < image[0].length) dfs(image, r, c + 1, color, newColor);
16+
}
17+
}*/
18+
19+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
20+
Queue<Node> queue = new LinkedList<Node>();
21+
int color = image[sr][sc];
22+
if (color == newColor) return image;
23+
queue.add(new Node(sr, sc));
24+
// BFS with queue
25+
while (!queue.isEmpty()) {
26+
Node curr = queue.remove();
27+
int r = curr.r, c = curr.c;
28+
if (image[r][c] == color) {
29+
image[r][c] = newColor;
30+
if (r - 1 >= 0) queue.add(new Node(r - 1, c));
31+
if (r + 1 < image.length) queue.add(new Node(r + 1, c));
32+
if (c - 1 >= 0) queue.add(new Node(r, c - 1));
33+
if (c + 1 < image[0].length) queue.add(new Node(r, c + 1));
34+
}
35+
}
36+
return image;
37+
}
38+
39+
class Node {
40+
int r;
41+
int c;
42+
43+
public Node(int r, int c) {
44+
this.r = r;
45+
this.c = c;
46+
}
47+
}
48+
}

python/733_Flood_Fill.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution(object):
2+
# def floodFill(self, image, sr, sc, newColor):
3+
# """
4+
# :type image: List[List[int]]
5+
# :type sr: int
6+
# :type sc: int
7+
# :type newColor: int
8+
# :rtype: List[List[int]]
9+
# """
10+
# r_ls, c_ls = len(image), len(image[0])
11+
# color = image[sr][sc]
12+
# if color == newColor:
13+
# return image
14+
15+
# def dfs(r, c):
16+
# if image[r][c] == color:
17+
# image[r][c] = newColor
18+
# if r - 1 >= 0: dfs(r - 1, c)
19+
# if r + 1 < r_ls: dfs(r + 1, c)
20+
# if c - 1 >= 0: dfs(r, c - 1)
21+
# if c + 1 < c_ls: dfs(r, c + 1)
22+
23+
# dfs(sr, sc)
24+
# return image
25+
26+
def floodFill(self, image, sr, sc, newColor):
27+
# BFS with queue
28+
r_ls, c_ls = len(image), len(image[0])
29+
color = image[sr][sc]
30+
if color == newColor:
31+
return image
32+
queue = [(sr, sc)]
33+
while len(queue) > 0:
34+
r, c = queue.pop(0)
35+
if image[r][c] == color:
36+
image[r][c] = newColor
37+
if r - 1 >= 0: queue.append((r - 1, c))
38+
if r + 1 < r_ls: queue.append((r + 1, c))
39+
if c - 1 >= 0: queue.append((r, c - 1))
40+
if c + 1 < c_ls: queue.append((r, c + 1))
41+
return image

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy