Skip to content

Commit af9956b

Browse files
author
Partho Biswas
committed
733_Flood_Fill
1 parent be619b6 commit af9956b

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
545545
|33| **[1192. Critical Connections in a Network](https://tinyurl.com/y6u2p7qw)** | [Python](https://tinyurl.com/wu6rdaw/1192_Critical_Connections_in_a_Network.py), [Swift](https://tinyurl.com/wuja3c4/1192_Critical_Connections_in_a_Network.swift) | [Art 1](https://tinyurl.com/yc2tzb4k), [Art 2](https://tinyurl.com/ycjd3rrr), [Art 3](https://tinyurl.com/ybj5nux8), [Art 4](https://tinyurl.com/y9wz5w8o), [Vid 1](https://tinyurl.com/y76cux54), [Vid 2](https://tinyurl.com/ybmgmv5l), [Vid 3](https://tinyurl.com/y7sa5oou) | Hard |📌 **Important, Learned new things.Tarjans SCCs algorithm. Check again** |
546546
|34| **[694. Number of Distinct Islands](https://tinyurl.com/ybf8cedn)** | [Python](https://tinyurl.com/wu6rdaw/694_Number_of_Distinct_Islands.py), [Swift](https://tinyurl.com/wuja3c4/694_Number_of_Distinct_Islands.swift) | [Art 1](https://tinyurl.com/y7rapyk6), [Art 2](https://tinyurl.com/yde8dqbb) | Medium |📌 |
547547
|35| [997. Find the Town Judge](https://tinyurl.com/y7875kls) | [Python](https://tinyurl.com/wu6rdaw/997_Find_the_Town_Judge.py), [Swift](https://tinyurl.com/wuja3c4/997_Find_the_Town_Judge.swift) | --- | Easy |📌 |
548+
|36| [733. Flood Fill](https://tinyurl.com/y9pecfd8) | [Python](https://tinyurl.com/wu6rdaw/733_Flood_Fill.py), [Swift](https://tinyurl.com/wuja3c4/733_Flood_Fill.swift) | --- | Easy |📌 |
548549

549550

550551
</p>

leetcode.com/python/733_Flood_Fill.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from collections import deque
2+
class Solution(object):
3+
def floodFill(self, image, sr, sc, newColor):
4+
"""
5+
:type image: List[List[int]]
6+
:type sr: int
7+
:type sc: int
8+
:type newColor: int
9+
:rtype: List[List[int]]
10+
"""
11+
queue = deque([(sr, sc)])
12+
startingColor = image[sr][sc]
13+
image[sr][sc] = newColor
14+
while queue:
15+
currentLevelSize = len(queue)
16+
while currentLevelSize > 0:
17+
currentLevelSize -= 1
18+
sourceR, sourceC = queue.popleft()
19+
neighbours = [(-1,0),(0,1),(1,0),(0,-1)]
20+
for neighbour in neighbours:
21+
dr, dc = neighbour
22+
newSR, newSC = sourceR + dr, sourceC + dc
23+
if 0 <= newSR < len(image) and 0 <= newSC < len(image[0]) and image[newSR][newSC] == startingColor and image[newSR][newSC] != newColor:
24+
queue.append((newSR, newSC))
25+
image[newSR][newSC] = newColor
26+
return image

0 commit comments

Comments
 (0)