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
59 changes: 59 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//o(mxn) time complexity
//o(mxn) space complexity
//bfs



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

Queue<int[]> queue = new LinkedList<>();
int m = grid.length;
int n = grid[0].length;
int freshOrangesCount = 0;

for(int i = 0; i< m; i++){
for (int j = 0; j < n ; j++){
if(grid[i][j] == 2){
queue.offer(new int[]{i,j});
}

if(grid[i][j] == 1) {
freshOrangesCount++;
}
}
}

if(freshOrangesCount == 0){
return 0;
}

if(queue.size() == 0){
return -1;
}

int minutes = -1;

int[][] dirs = {{1,0}, {-1,0},{0,-1},{0,1} };

while(!queue.isEmpty()){
int size = queue.size();
minutes++;
for(int k =0; k< size; k++){
int[] curr = queue.poll();
int r = curr[0];
int c = curr[1];
for(int[] dir: dirs){
int nr = r + dir[0];
int nc = c + dir[1];
if(nr >= 0 && nr < m && nc >=0 && nc < n && grid[nr][nc] == 1){
grid[nr][nc] = 2;
freshOrangesCount--;
queue.offer(new int[]{nr,nc});
}
}
}
}
return freshOrangesCount == 0 ? minutes : -1;
}
}
41 changes: 41 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//bfs
//o(n) space complexity
//o(n) time complexity
/*
// Definition for Employee.
class Employee {
public int id;
public int importance;
public List<Integer> subordinates;
};
*/

class Solution {
HashMap<Integer, Employee> map = new HashMap<>();
public int getImportance(List<Employee> employees, int id) {

int impTotal = 0;

Queue<Integer> q = new LinkedList<>();

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


while(!q.isEmpty()){
int size = q.size();
int curr = q.poll();
Employee currEmp = map.get(curr);

impTotal = impTotal+currEmp.importance;

for(int empId: currEmp.subordinates){
q.add(empId);

}
}
return impTotal;
}
}