Skip to content
Open

BFS-2 #612

Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions employee-importance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
// Definition for Employee.
class Employee {
public int id;
public int importance;
public IList<int> subordinates;
}
*/

// Time Complexity: O(n)
// Space Complexity: O(n)

// Build a dictionary to quickly map employee id & Employee object.
// Use a queue (BFS) starting from the given id to traverse the employee and all their subordinates.
// Sum the importance of each visited employee and return the total.
class Solution
{
public int GetImportance(IList<Employee> employees, int id)
{
Queue<int> q = new();
q.Enqueue(id);
int result = 0;
Dictionary<int, Employee> dict = new();
foreach (var emp in employees)
{
dict.Add(emp.id, emp);
}
while (q.Count != 0)
{
int cid = q.Dequeue();
Employee abc = dict[cid];
result += abc.importance;
foreach (var ab in abc.subordinates)
{
q.Enqueue(ab);
}

}
return result;
}
}
68 changes: 68 additions & 0 deletions rotting-oranges.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

// Time Complexity: O(m × n)
// Space Complexity: O(m × n)

// Approach
// Initially add all rotten oranges to a queue and count fresh oranges (multi-source BFS).
// Perform BFS level by level; each level represents one minute, rotting adjacent fresh oranges.
// If all fresh oranges rot, return total minutes taken; otherwise return -1.
public class Solution
{
public int OrangesRotting(int[][] grid)
{

int[][] directions = new int[][]{new int[]{1,0},new int[]{-1,0},
new int[]{0,1},new int[]{0,-1}};
int time = 0;
int freshOranges = 0;
int rows = grid.Length;
int cols = grid[0].Length;
Queue<int[]> q = new();

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (grid[i][j] == 2)
{
q.Enqueue(new int[] { i, j });
}
if (grid[i][j] == 1)
{
freshOranges++;
}
}
}
if (freshOranges == 0) return 0;

while (q.Count > 0)
{
int size = q.Count();
for (int i = 0; i < size; i++)
{
int[] cur = q.Dequeue();
int startRow = cur[0];
int startCol = cur[1];

foreach (int[] dir in directions)
{
int curRow = startRow + dir[0];
int curCol = startCol + dir[1];

if ((curRow >= 0 && curRow < rows) && (curCol >= 0 && curCol < cols)
&& grid[curRow][curCol] == 1)
{
grid[curRow][curCol] = 2;
freshOranges--;
q.Enqueue(new int[] { curRow, curCol });
}
}
}
time++;
}
return freshOranges == 0 ? time - 1 : -1;
}
}