diff --git a/Problem-1.java b/Problem-1.java new file mode 100644 index 0000000..62055b4 --- /dev/null +++ b/Problem-1.java @@ -0,0 +1,67 @@ +// Time Complexity : O(M × N) +// Space Complexity : O(M × N) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Approach: We use multi-source BFS by first adding all initially rotten oranges +// into the queue and counting fresh oranges. Each BFS level represents 1 minute, +// and we rot all adjacent fresh oranges while decreasing the fresh count. +// At the end, if no fresh oranges remain, we return the total minutes taken; +// otherwise, we return -1. + + +import java.util.*; + +class Solution { + public int orangesRotting(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + + int[][] dirs = {{0,1}, {1,0}, {0,-1}, {-1,0}}; + Queue queue = new LinkedList<>(); + int freshCount = 0; + int minutes = 0; + + // Step 1: Add all rotten oranges to queue and count fresh ones + 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) { + freshCount++; + } + } + } + + // Step 2: Multi-source BFS + while (!queue.isEmpty()) { + int size = queue.size(); + boolean rottedThisRound = false; + + for (int i = 0; i < size; i++) { + int[] top = queue.poll(); + + for (int[] dir : dirs) { + int nr = top[0] + dir[0]; + int nc = top[1] + dir[1]; + + if (nr >= 0 && nr < m && nc >= 0 && nc < n) { + if (grid[nr][nc] == 1) { + grid[nr][nc] = 2; // mark rotten + freshCount--; + queue.offer(new int[]{nr, nc}); + rottedThisRound = true; + } + } + } + } + + if (rottedThisRound) { + minutes++; + } + } + + return freshCount == 0 ? minutes : -1; + } +} diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 0000000..cf906c2 --- /dev/null +++ b/Problem-1.py @@ -0,0 +1,57 @@ +# Time Complexity : O(M × N) +# Space Complexity : O(M × N) +# Did this code successfully run on Leetcode : yes +# Any problem you faced while coding this : no + +# Approach: We use multi-source BFS by first adding all initially rotten oranges +# into the queue and counting fresh oranges. Each BFS level represents 1 minute, +# and we rot all adjacent fresh oranges while decreasing the fresh count. +# At the end, if no fresh oranges remain, we return the total minutes taken; +# otherwise, we return -1. + +class Solution: + def orangesRotting(self, grid: List[List[int]]) -> int: + m = len(grid) + n = len(grid[0]) + dirs = [[0,1],[1,0],[0,-1],[-1,0]] + result = 0 + queue = deque() + count = 0 + for i in range(m): + for j in range(n): + if grid[i][j] == 2: + queue.append([i,j]) + if grid[i][j] == 1: + count += 1 + while queue: + size = len(queue) + for _ in range(size): + top = queue.popleft() + for dir in dirs: + nr = top[0]+dir[0] + nc = top[1]+ dir[1] + if nr >= 0 and nr < len(grid) and nc >= 0 and nc < len(grid[0]): + if grid[nr][nc] == 1: + count -= 1 + grid[nr][nc] = -1 + queue.append([nr,nc]) + result += 1 + if count == 0 and result!= 0: + return result-1 + elif count == 0: + return 0 + + return -1 + + + + + + + + + + + + + \ No newline at end of file diff --git a/Problem-2.java b/Problem-2.java new file mode 100644 index 0000000..ce96743 --- /dev/null +++ b/Problem-2.java @@ -0,0 +1,38 @@ +// Time Complexity : O(N) where N is the number of employees +// Space Complexity : O(N) for the hashmap and recursion/queue +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Approach: We first create a hashmap mapping employee id to Employee object for O(1) lookups. +// Then we perform DFS or BFS starting from the given employee id, summing the importance +// of that employee and all their direct and indirect subordinates. This efficiently computes +// total importance without repeatedly scanning the employee list. + +import java.util.*; + +class Solution { + public int getImportance(List employees, int id) { + // Map id -> Employee + Map map = new HashMap<>(); + for (Employee emp : employees) { + map.put(emp.id, emp); + } + + int totalImportance = 0; + Queue queue = new LinkedList<>(); + queue.offer(id); + + while (!queue.isEmpty()) { + int currentId = queue.poll(); + Employee employee = map.get(currentId); + + totalImportance += employee.importance; + + for (int subId : employee.subordinates) { + queue.offer(subId); + } + } + + return totalImportance; + } +} diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 0000000..115e0f9 --- /dev/null +++ b/Problem-2.py @@ -0,0 +1,29 @@ +# Time Complexity : O(N) where N is the number of employees +# Space Complexity : O(N) for the hashmap and recursion/queue +# Did this code successfully run on Leetcode : yes +# Any problem you faced while coding this : no + +# Approach: We first create a hashmap mapping employee id to Employee object for O(1) lookups. +# Then we perform DFS or BFS starting from the given employee id, summing the importance +# of that employee and all their direct and indirect subordinates. This efficiently computes +# total importance without repeatedly scanning the employee list. + +from collections import deque + +class Solution: + def getImportance(self, employees: List['Employee'], id: int) -> int: + emp_map = {emp.id: emp for emp in employees} + + total_importance = 0 + queue = deque([id]) + + while queue: + current_id = queue.popleft() + employee = emp_map[current_id] + + total_importance += employee.importance + + for sub_id in employee.subordinates: + queue.append(sub_id) + + return total_importance