Skip to content
Open
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
84 changes: 84 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
Time Complexity : O(M * N)
Explanation:
We scan the grid once to find initial rotten and fresh oranges.
Then each cell is processed at most once during BFS.

Space Complexity : O(M * N)
Explanation:
Queue may contain many cells in worst case when oranges rot level by level.

Did this code successfully run on LeetCode : Yes

Any problem you faced while coding this :
Initially struggled with tracking the time correctly during BFS levels.
Fixed it by processing the queue level by level (each level = 1 minute).
Also needed to keep track of the number of fresh oranges so we know
when all oranges have rotted and can return the time immediately.
*/


class Solution {


public int orangesRotting(int[][] grid) {

int m = grid.length;
int n = grid[0].length;

// Directions: right, left, up, down
int[][] adj = {{0,1}, {0,-1}, {-1,0}, {1,0}};

Queue<int[]> q = new LinkedList<>();
int fresh = 0;
int time = 0;

// Step 1: Find all rotten oranges and count fresh oranges
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {

if (grid[i][j] == 2) {
q.add(new int[]{i, j});
}
else if (grid[i][j] == 1) {
fresh++;
}
}
}

if (fresh == 0) return 0;

// Step 2: BFS to rot adjacent fresh oranges
while (!q.isEmpty()) {

int size = q.size();

for (int i = 0; i < size; i++) {

int[] rotten = q.poll();

for (int[] dir : adj) {

int nr = rotten[0] + dir[0];
int nc = rotten[1] + dir[1];

if (nr >= 0 && nr < m && nc >= 0 && nc < n) {

if (grid[nr][nc] == 1) {

grid[nr][nc] = 2;
q.add(new int[]{nr, nc});
fresh--;

if (fresh == 0) return time + 1;
}
}
}
}

time++;
}

return -1;
}
}
67 changes: 67 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

/**
Time Complexity : O(N)
Explanation:
We first build a HashMap of employees in O(N).
Then BFS traverses each employee and their subordinates once.

Space Complexity : O(N)
Explanation:
HashMap stores all employees and queue stores employees during BFS traversal.

Did this code successfully run on LeetCode : Yes

Any problem you faced while coding this :
Initially it was difficult to access employees by their id quickly.
Solved it by creating a HashMap that maps employee id to Employee object.
Then used BFS starting from the given id and added the importance
of the employee and all their subordinates.
*/

/*
// Definition for Employee.
class Employee {
public int id;
public int importance;
public List<Integer> subordinates;
};
*/

class Solution {


public int getImportance(List<Employee> employees, int id) {

Queue<Integer> q = new LinkedList<>();
int n = employees.size();

HashMap<Integer, Employee> hmap = new HashMap<>();

// Build id -> Employee map
for (int i = 0; i < n; i++) {
Employee e = employees.get(i);
hmap.put(e.id, e);

if (id == e.id) {
q.add(e.id);
}
}

int total = 0;

// BFS through employee hierarchy
while (!q.isEmpty()) {

int curr = q.poll();
Employee emp = hmap.get(curr);

total += emp.importance;

for (Integer sub : emp.subordinates) {
q.add(sub);
}
}

return total;
}
}