From 15ea67cde9b73a6d101d504676760058147e4de0 Mon Sep 17 00:00:00 2001 From: rvuyyuru7 Date: Mon, 9 Feb 2026 19:17:18 -0600 Subject: [PATCH 1/2] Rotting oranges complete --- RottingOranges.java | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 RottingOranges.java diff --git a/RottingOranges.java b/RottingOranges.java new file mode 100644 index 0000000..9c60ebc --- /dev/null +++ b/RottingOranges.java @@ -0,0 +1,60 @@ +class Solution { + // Approach: BFS + // TC: O(m * n) + // SC: O(m * n) for queue + private static final int EMPTY = 0; + private static final int FRESH = 1; + private static final int ROTTEN = 2; + private static final int[][] DIRECTIONS = new int[][] {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; + public int orangesRotting(int[][] grid) { + int minNumOfMinutes = 0; + int countOfFreshOranges = 0; + // Add rotten orange indices to queue and maintain count of fresh oranges. + Queue indicesOfRottenOranges = new LinkedList<>(); + int numOfRows = grid.length; + int numOfColumns = grid[0].length; + for (int row = 0; row < numOfRows; row ++) { + for (int column = 0; column < numOfColumns; column ++) { + if (grid[row][column] == ROTTEN) { + indicesOfRottenOranges.offer(new int[] { row, column }); // add index + } else if (grid[row][column] == FRESH) { + countOfFreshOranges ++; // increment count of fresh oranges + } + } + } + // No fresh oranges. + if (countOfFreshOranges == 0) { + return minNumOfMinutes; + } + // Iterate through all rotten oranges and rotten their neighbors. + int countOfNewlyRottenOranges = 0; + while (!indicesOfRottenOranges.isEmpty()) { + int levelSize = indicesOfRottenOranges.size(); + while (levelSize -- != 0) { + int[] rottenOrangeIndex = indicesOfRottenOranges.poll(); + int row = rottenOrangeIndex[0]; + int column = rottenOrangeIndex[1]; + // Check neighbors on all 4 directions. + for (int[] direction: DIRECTIONS) { + int newRow = row + direction[0]; + int newColumn = column + direction[1]; + // Rotten the fresh orange. + if (newRow >= 0 && newColumn >= 0 && newRow < numOfRows && newColumn < numOfColumns + && grid[newRow][newColumn] == FRESH + ) { + grid[newRow][newColumn] = ROTTEN; + indicesOfRottenOranges.offer(new int[] { newRow, newColumn }); + countOfNewlyRottenOranges ++; + } + } + } + minNumOfMinutes ++; // increment after each level + if (countOfFreshOranges == countOfNewlyRottenOranges) { + // All oranges are rotten. + return minNumOfMinutes; + } + } + // Unable to rotten all oranges. + return -1; + } +} \ No newline at end of file From 8a7b4104861debe7212ec475b83701d8901acfa7 Mon Sep 17 00:00:00 2001 From: rvuyyuru7 Date: Wed, 11 Feb 2026 22:31:38 -0600 Subject: [PATCH 2/2] Employee importance done --- EmployeeImportance.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 EmployeeImportance.java diff --git a/EmployeeImportance.java b/EmployeeImportance.java new file mode 100644 index 0000000..8f4ce86 --- /dev/null +++ b/EmployeeImportance.java @@ -0,0 +1,35 @@ +/* +// Definition for Employee. +class Employee { + public int id; + public int importance; + public List subordinates; +}; +*/ + +// Approach: Employee map + BFS with queue +// TC: N for populating employeeMap + N for iterating through subordinatesQueue ~= 2N ~= O(N) +// SC: N for employeeMap + N for subordinatesQueue ~= 2N ~= O(N) +class Solution { + public int getImportance(List employees, int id) { + Map employeeMap = new HashMap<>(); + for (Employee employee: employees) { + employeeMap.put(employee.id, employee); + } + Queue subordinatesQueue = new LinkedList<>(); + subordinatesQueue.offer(id); + int importance = 0; + while (!subordinatesQueue.isEmpty()) { + int subordinateId = subordinatesQueue.poll(); + if (employeeMap.containsKey(subordinateId)) { + Employee employee = employeeMap.get(subordinateId); + importance += employee.importance; + List subordinates = employee.subordinates; + for (int subordinate: subordinates) { + subordinatesQueue.offer(subordinate); + } + } + } + return importance; + } +} \ No newline at end of file