Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Comment thread
prem043m marked this conversation as resolved.
52 changes: 52 additions & 0 deletions C++/FloodFillDFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>
#include <vector>
using namespace std;

class FloodFillDFS {
public:
void floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
Comment thread
prem043m marked this conversation as resolved.
int oldColor = image[sr][sc];
if (oldColor == newColor) return;
dfs(image, sr, sc, oldColor, newColor);
}

void dfs(vector<vector<int>>& image, int r, int c, int oldColor, int newColor) {
// Boundary check
if (r < 0 || c < 0 || r >= image.size() || c >= image[0].size())
Comment thread
prem043m marked this conversation as resolved.
Outdated
return;

// Stop if color does not match
if (image[r][c] != oldColor)
return;

// Replace color
image[r][c] = newColor;

// Explore neighbors
dfs(image, r + 1, c, oldColor, newColor);
dfs(image, r - 1, c, oldColor, newColor);
dfs(image, r, c + 1, oldColor, newColor);
dfs(image, r, c - 1, oldColor, newColor);
}
};

int main() {
vector<vector<int>> image = {
{1, 1, 1},
{1, 1, 0},
{1, 0, 1}
};

FloodFillDFS solver;
solver.floodFill(image, 1, 1, 2);

// Print output
for (auto& row : image) {
for (int val : row) {
cout << val << " ";
}
cout << endl;
}

return 0;
}
Binary file added C++/FloodFillDFS.exe
Binary file not shown.
5 changes: 5 additions & 0 deletions README_FLOOD_FILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Flood Fill Algorithm (LeetCode 733)

- Uses DFS to fill connected cells of the same color.
- Time: O(R × C)
- Space: O(R × C)
Comment thread
prem043m marked this conversation as resolved.
Binary file added java/FloodFillBFS.class
Binary file not shown.
37 changes: 37 additions & 0 deletions java/FloodFillDFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
public class FloodFillDFS {

Comment thread
prem043m marked this conversation as resolved.
Outdated
public static void main(String[] args) {
int[][] image = {
{1, 1, 1},
{1, 1, 0},
{1, 0, 1}
};

floodFill(image, 1, 1, 2);

for (int[] row : image) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}

static void floodFill(int[][] image, int sr, int sc, int newColor) {
Comment thread
prem043m marked this conversation as resolved.
int oldColor = image[sr][sc];
if (oldColor == newColor) return;
dfs(image, sr, sc, oldColor, newColor);
}

static void dfs(int[][] image, int r, int c, int oldColor, int newColor) {
if (r < 0 || c < 0 || r >= image.length || c >= image[0].length) return;
if (image[r][c] != oldColor) return;

image[r][c] = newColor;

dfs(image, r + 1, c, oldColor, newColor);
dfs(image, r - 1, c, oldColor, newColor);
dfs(image, r, c + 1, oldColor, newColor);
dfs(image, r, c - 1, oldColor, newColor);
}
}
Binary file added java/Solution.class
Binary file not shown.