Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

141. Flood Fill

EasyDay 42 of 60Graphs

An image is a grid of integers image[i][j] (pixel colours). Starting from pixel (sr, sc), perform a flood fill: change the starting pixel and every pixel connected to it horizontally or vertically through pixels of the same original colour to color. Return the modified image.

Example 1
Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
The bottom right 1 is not connected to the start through 1s, so it keeps its colour.
Example 2
Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
Output: [[0,0,0],[0,0,0]]
The new colour equals the old one, so nothing changes.
Constraints
  • 1 <= m, n <= 50
  • 0 <= image[i][j], color < 2^16
  • 0 <= sr < m, 0 <= sc < n
Hints
Frequently asked at: Amazon, Google, Microsoft
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min