diff --git a/Course Schedule II - Leetcode 210/Course Schedule II - Leetcode 210.py b/Course Schedule II - Leetcode 210/Course Schedule II - Leetcode 210.py index ed8bfab..cd8d244 100644 --- a/Course Schedule II - Leetcode 210/Course Schedule II - Leetcode 210.py +++ b/Course Schedule II - Leetcode 210/Course Schedule II - Leetcode 210.py @@ -7,7 +7,6 @@ def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int UNVISITED, VISITING, VISITED = 0, 1, 2 states = [UNVISITED] * numCourses - def dfs(i): if states[i] == VISITING: diff --git a/Daily Temperatures - Leetcode 739/Daily Temperatures - Leetcode 739.py b/Daily Temperatures - Leetcode 739/Daily Temperatures - Leetcode 739.py index b39b3cb..8fd91a4 100644 --- a/Daily Temperatures - Leetcode 739/Daily Temperatures - Leetcode 739.py +++ b/Daily Temperatures - Leetcode 739/Daily Temperatures - Leetcode 739.py @@ -1,3 +1,19 @@ +# Brute Force +class Solution: + def dailyTemperatures(self, temperatures: List[int]) -> List[int]: + n = len(temperatures) + answer = [0] * n + for i in range(n): + for j in range(i+1, n): + if temperatures[j] > temperatures[i]: + answer[i] = j - i + break + + return answer +# Time: O(n^2) +# Space: O(1) + +# Optimal class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: temps = temperatures diff --git a/Linked List Cycle - Leetcode 141/Linked List Cycle - Leetcode 141.py b/Linked List Cycle - Leetcode 141/Linked List Cycle - Leetcode 141.py index da86af6..d0b3fd7 100644 --- a/Linked List Cycle - Leetcode 141/Linked List Cycle - Leetcode 141.py +++ b/Linked List Cycle - Leetcode 141/Linked List Cycle - Leetcode 141.py @@ -1,8 +1,6 @@ class Solution: def hasCycle(self, head: Optional[ListNode]) -> bool: - dummy = ListNode() - dummy.next = head - slow = fast = dummy + slow = fast = head while fast and fast.next: fast = fast.next.next diff --git a/Rotting Oranges - Leetcode 994/Rotting Oranges - Leetcode 994.py b/Rotting Oranges - Leetcode 994/Rotting Oranges - Leetcode 994.py index 32f3ce0..c14eb3f 100644 --- a/Rotting Oranges - Leetcode 994/Rotting Oranges - Leetcode 994.py +++ b/Rotting Oranges - Leetcode 994/Rotting Oranges - Leetcode 994.py @@ -1,3 +1,40 @@ +# Code for Bootcamp +class Solution: + from collections import deque + + def orangesRotting(self, grid): + # Initialize variables + Minute = 0 + Q = deque() + FreshCount = 0 + M, N = len(grid), len(grid[0]) + + # Populate queue with initial rotten oranges and count fresh oranges + for i in range(M): + for j in range(N): + if grid[i][j] == 2: + Q.append((i, j)) + elif grid[i][j] == 1: + FreshCount += 1 + + # Perform BFS + while Q and FreshCount > 0: + NumRotting = len(Q) + for _ in range(NumRotting): + i, j = Q.popleft() + for r, c in [(i, j + 1), (i + 1, j), (i, j - 1), (i - 1, j)]: + if 0 <= r < M and 0 <= c < N and grid[r][c] == 1: + grid[r][c] = 2 + FreshCount -= 1 + Q.append((r, c)) + Minute += 1 # Increment minute after processing all rotten oranges in this round + + # Return the time taken or -1 if fresh oranges remain + return Minute if FreshCount == 0 else -1 + + + +# Code for YouTube Video from collections import deque class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: