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
49 changes: 49 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Problem1 {
int[][] dirs;
int m,n;

public int orangesRotting(int[][] grid) {
this.dirs = new int[][]{{-1,0},{1,0},{0,1},{0,-1}};
this.m = grid.length;
this.n = grid[0].length;
int fresh = 0;

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

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++;
}
}
}
int time = 0;

if(fresh == 0) return time;

while(!q.isEmpty()){
int size = q.size();
time++;
for(int i=0; i<size; i++){
int[] curr = q.poll();
for(int[] dir: dirs){ //
int r = curr[0] + dir[0];
int c = curr[1] + dir[1];

if(r>=0 && c>=0 && r<m && c<n && grid[r][c] == 1){
grid[r][c] = 2;
q.add(new int[]{r,c});
fresh--;
if(fresh == 0) return time;
}
}
}

}

// if(fresh == 0) return time-1;
return -1;
}
}
39 changes: 39 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
// Definition for Employee.
class Employee {
public int id;
public int importance;
public List<Integer> subordinates;
};
*/

class Problem2 {
HashMap<Integer, Employee> map;

public int getImportance(List<Employee> employees, int id) {
this.map = new HashMap<>();

for(Employee emp: employees){
map.put(emp.id, emp);
}

Queue<Integer> q = new LinkedList<>();
q.add(id);

int result = 0;

while(!q.isEmpty()){
int currId = q.poll();

Employee currObj = map.get(currId);

result += currObj.importance;

for(int subId : currObj.subordinates){
q.add(subId);
}
}

return result;
}
}