From e336bbde25692d293160fadaec1c0ef0f2969fff Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Wed, 9 Oct 2024 10:02:47 -0400 Subject: [PATCH 01/55] Update Contains Duplicate - Leetcode 217.py --- .../Contains Duplicate - Leetcode 217.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py b/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py index c3897ef..216fb44 100644 --- a/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py +++ b/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py @@ -1,3 +1,20 @@ +# Brute Force Solution +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + n = len(nums) + + for i in range(n): + for j in range(n): + if i == j: + continue + elif nums[i] == nums[j]: + return True + + return False + # Time Complexity: O(n^2) + # Space Complexity: O(1) + +# Optimal Solution class Solution: def containsDuplicate(self, nums: list[int]) -> bool: h = set() From e7b5e89b6a7ffee31d5d7dfdbcf3df5c86c8807f Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Wed, 9 Oct 2024 15:45:52 -0400 Subject: [PATCH 02/55] Update Merge Strings Alternately - Leetcode 1768.py --- ...rge Strings Alternately - Leetcode 1768.py | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/Merge Strings Alternately - Leetcode 1768/Merge Strings Alternately - Leetcode 1768.py b/Merge Strings Alternately - Leetcode 1768/Merge Strings Alternately - Leetcode 1768.py index ffd2397..ac9a73d 100644 --- a/Merge Strings Alternately - Leetcode 1768/Merge Strings Alternately - Leetcode 1768.py +++ b/Merge Strings Alternately - Leetcode 1768/Merge Strings Alternately - Leetcode 1768.py @@ -1,3 +1,38 @@ +# Brute Force Solution +class Solution: + def mergeAlternately(self, word1: str, word2: str) -> str: + characters = "" + cur_word = 1 + a, b = 0, 0 + + while a < len(word1) and b < len(word2): + if cur_word == 1: + characters += word1[a] + a += 1 + cur_word = 2 + else: + characters += word2[b] + b += 1 + cur_word = 1 + + while a < len(word1): + characters += word1[a] + a += 1 + + while b < len(word2): + characters += word2[b] + b += 1 + + return characters + # Let A be the length of Word1 + # Let B be the length of Word2 + # Let T = A + B + + # Time: O(T^2) + # Space: O(T) + + +# Optimal Solution class Solution: def mergeAlternately(self, word1: str, word2: str) -> str: A, B = len(word1), len(word2) @@ -24,5 +59,9 @@ def mergeAlternately(self, word1: str, word2: str) -> str: b += 1 return ''.join(s) - # Time: O(A + B) - A is Length of word1, B is Length of word2 - # Space: O(A + B) - A is Length of word1, B is Length of word2 + # Let A be the length of Word1 + # Let B be the length of Word2 + # Let T = A + B + + # Time: O(T) + # Space: O(T) From 4ee2d06f81a0b785aa1d41f6d2d5daaae4aaec5f Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 10 Oct 2024 09:29:40 -0400 Subject: [PATCH 03/55] Update Product of Array Except Self - Leetcode 238.py --- ...uct of Array Except Self - Leetcode 238.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py b/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py index a0232d4..d072aff 100644 --- a/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py +++ b/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py @@ -1,3 +1,24 @@ +# Brute Force Solution +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + n = len(nums) + ans = [0] * n + + for i in range(n): + multiplier = 1 + for j in range(n): + if i == j: + continue + else: + multiplier *= nums[j] + ans[i] = multiplier + + return ans + # Time: O(n^2) + # Space: O(n) + + +# Optimal Solution class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: l_mult = 1 From 310dc1c0c33137c6e263c0c47e683301d6c6b1f7 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 10 Oct 2024 10:32:13 -0400 Subject: [PATCH 04/55] Update Product of Array Except Self - Leetcode 238.py --- ...uct of Array Except Self - Leetcode 238.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py b/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py index d072aff..a140a3f 100644 --- a/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py +++ b/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py @@ -38,3 +38,33 @@ def productExceptSelf(self, nums: List[int]) -> List[int]: # Time Complexity: O(n) # Space Complexity: O(n) + + +# Optimal Solution for Bootcamp +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + n = len(nums) + answer = [0] * n + + L = 1 + left_product = [0] * n + + for i in range(n): + left_product[i] = L + L *= nums[i] + + + R = 1 + right_product = [0] * n + + for i in range(n-1, -1, -1): + right_product[i] = R + R *= nums[i] + + + for i in range(n): + answer[i] = left_product[i] * right_product[i] + + return answer +# Time Complexity: O(n) +# Space Complexity: O(n) From 188b76ab8e05f9077f5882a1291da7a0809051e5 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 10 Oct 2024 14:28:24 -0400 Subject: [PATCH 05/55] Update Jewels and Stones - Leetcode 771.py --- .../Jewels and Stones - Leetcode 771.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Jewels and Stones - Leetcode 771/Jewels and Stones - Leetcode 771.py b/Jewels and Stones - Leetcode 771/Jewels and Stones - Leetcode 771.py index 1b5c794..2aa2180 100644 --- a/Jewels and Stones - Leetcode 771/Jewels and Stones - Leetcode 771.py +++ b/Jewels and Stones - Leetcode 771/Jewels and Stones - Leetcode 771.py @@ -1,3 +1,17 @@ +# Brute Force Solution +class Solution: + def numJewelsInStones (self, jewels: str, stones: str) -> int: + count = 0 + for stone in stones: + if stone in jewels: + count += 1 + return count + +# Time Complexity: O(n * m) +# Space Complexity: O(1) + + +# Optimal Solution class Solution: def numJewelsInStones (self, jewels: str, stones: str) -> int: # O(n + m) From dc7557d7d54f10ac113db1dd8f6db87d8d083122 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 10 Oct 2024 21:02:15 -0400 Subject: [PATCH 06/55] Update Contains Duplicate - Leetcode 217.py --- .../Contains Duplicate - Leetcode 217.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py b/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py index 216fb44..d245fea 100644 --- a/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py +++ b/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py @@ -4,10 +4,8 @@ def containsDuplicate(self, nums: List[int]) -> bool: n = len(nums) for i in range(n): - for j in range(n): - if i == j: - continue - elif nums[i] == nums[j]: + for j in range(i+1, n): + if nums[i] == nums[j]: return True return False From d31fb20f800d1f221d9c79d106cee497cc35b756 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 14 Nov 2024 13:41:35 -0500 Subject: [PATCH 07/55] Update Ransom Note - Leetcode 383.py --- .../Ransom Note - Leetcode 383.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.py b/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.py index fba5a04..3f5de88 100644 --- a/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.py +++ b/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.py @@ -1,3 +1,18 @@ +# Brute Force Solution +class Solution: + def canConstruct(self, ransomNote: str, magazine: str) -> bool: + for letter in ransomNote: + if letter in magazine: + position = magazine.index(letter) + magazine = magazine[:position] + magazine[position+1:] + else: + return False + + return True +# Time: O(R * M) +# Space: O(1) + +# Optimal Solution class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: hashmap = Counter(magazine) # TC for Counter is O(n) @@ -9,5 +24,5 @@ def canConstruct(self, ransomNote: str, magazine: str) -> bool: return False return True -# Time Complexity: O(m + n) -> m = len(ransomNote), n = len(magazine) -# Space Complexity: O(n) -> we're using a hashmap \ No newline at end of file +# Time Complexity: O(R + M) -> R = len(ransomNote), M = len(magazine) +# Space Complexity: O(M) -> we're using a hashmap From 4cc7b255245dc5222f41b7854597dcfb094a0306 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 14 Nov 2024 13:59:30 -0500 Subject: [PATCH 08/55] Update Contains Duplicate - Leetcode 217.py --- .../Contains Duplicate - Leetcode 217.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py b/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py index d245fea..9026a1b 100644 --- a/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py +++ b/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py @@ -4,10 +4,9 @@ def containsDuplicate(self, nums: List[int]) -> bool: n = len(nums) for i in range(n): - for j in range(i+1, n): - if nums[i] == nums[j]: + for j in range(n): + if nums[i] == nums[j] and i != j: return True - return False # Time Complexity: O(n^2) # Space Complexity: O(1) From d29cd550de257be99031cbe272e85ab0b356b634 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 14 Nov 2024 14:32:54 -0500 Subject: [PATCH 09/55] Update Product of Array Except Self - Leetcode 238.py --- .../Product of Array Except Self - Leetcode 238.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py b/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py index a140a3f..dc4a630 100644 --- a/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py +++ b/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py @@ -7,9 +7,7 @@ def productExceptSelf(self, nums: List[int]) -> List[int]: for i in range(n): multiplier = 1 for j in range(n): - if i == j: - continue - else: + if i != j: multiplier *= nums[j] ans[i] = multiplier From 4dfbc4924af2841cbafbf4f49cb5868c7f5d42e9 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 14 Nov 2024 14:36:48 -0500 Subject: [PATCH 10/55] Update Product of Array Except Self - Leetcode 238.py --- .../Product of Array Except Self - Leetcode 238.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py b/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py index dc4a630..63c25b1 100644 --- a/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py +++ b/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py @@ -5,11 +5,11 @@ def productExceptSelf(self, nums: List[int]) -> List[int]: ans = [0] * n for i in range(n): - multiplier = 1 + prod = 1 for j in range(n): if i != j: - multiplier *= nums[j] - ans[i] = multiplier + prod *= nums[j] + ans[i] = prod return ans # Time: O(n^2) From f176b79300e8e21c23063f9a1949db6aada121e8 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 14 Nov 2024 17:38:09 -0500 Subject: [PATCH 11/55] Update Group Anagrams - Leetcode 49.py --- .../Group Anagrams - Leetcode 49.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Group Anagrams - Leetcode 49/Group Anagrams - Leetcode 49.py b/Group Anagrams - Leetcode 49/Group Anagrams - Leetcode 49.py index 28e4bda..06af914 100644 --- a/Group Anagrams - Leetcode 49/Group Anagrams - Leetcode 49.py +++ b/Group Anagrams - Leetcode 49/Group Anagrams - Leetcode 49.py @@ -1,3 +1,17 @@ +# Brute Force Solution +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + ans = defaultdict(list) + for s in strs: + key = ''.join(sorted(s)) + ans[key].append(s) + + return list(ans.values()) +# Time: O(n * (m log m)) +# Space: O(n * m) +# n is the number of strings, m is the length of largest string + +# Optimal Solution from collections import defaultdict class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: From 7973e602c235cb7125d1eec8830ec737517d453b Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Fri, 15 Nov 2024 09:35:06 -0500 Subject: [PATCH 12/55] Update Squares of a Sorted Array - Leetcode 977.py --- .../Squares of a Sorted Array - Leetcode 977.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py b/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py index 1174376..d26597f 100644 --- a/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py +++ b/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py @@ -1,3 +1,19 @@ +# Brute Force Solution +class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + n = len(nums) + for i in range(n): + nums[i] = nums[i] ** 2 + + nums.sort() + + return nums + +# Time: O(n log n) +# Space: O(1) + + +# Optimal Solution class Solution: def sortedSquares(self, nums: List[int]) -> List[int]: left = 0 From 182550e6722c7276ce7fc4ca9fc67478f7ccd5d8 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Fri, 15 Nov 2024 09:44:40 -0500 Subject: [PATCH 13/55] Update Squares of a Sorted Array - Leetcode 977.py --- .../Squares of a Sorted Array - Leetcode 977.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py b/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py index d26597f..aaeaf6d 100644 --- a/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py +++ b/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py @@ -33,4 +33,4 @@ def sortedSquares(self, nums: List[int]) -> List[int]: return result # Time Complexity: O(n) -# Space Complexity: O(1) +# Space Complexity: O(n) From b44ea2bd66c578540df561d9662a7b1bcf9082a5 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Fri, 15 Nov 2024 09:56:34 -0500 Subject: [PATCH 14/55] Update Squares of a Sorted Array - Leetcode 977.py --- ...quares of a Sorted Array - Leetcode 977.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py b/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py index aaeaf6d..ed1c9bd 100644 --- a/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py +++ b/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py @@ -34,3 +34,26 @@ def sortedSquares(self, nums: List[int]) -> List[int]: # Time Complexity: O(n) # Space Complexity: O(n) + +# Optimal Solution for Bootcamp +class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + n = len(nums) + L, R = 0, n-1 + result = [] + + for i in range(n): + nums[i] = nums[i] ** 2 + + while L <= R: + if nums[L] > nums[R]: + result.append(nums[L]) + L += 1 + else: + result.append(nums[R]) + R -= 1 + + result.reverse() + return result +# Time: O(n) +# Space: O(n) From a8eb54bf2647499277d2d1e3756cdc05e0533085 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Fri, 15 Nov 2024 15:36:41 -0500 Subject: [PATCH 15/55] Update Search a 2D Matrix - Leetcode 74.py --- .../Search a 2D Matrix - Leetcode 74.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py b/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py index c45100a..d72163f 100644 --- a/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py +++ b/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py @@ -1,3 +1,15 @@ +# Brute Force Solution +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + for row in matrix: + if target in row: + return True + + return False +# Time: O(m * n) +# Space: O(1) + +# Optimal Solution class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: m = len(matrix) From 19b1fb0d906ced50f8ce1414a79ce7b3494f48ce Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Fri, 15 Nov 2024 15:54:47 -0500 Subject: [PATCH 16/55] Update Search a 2D Matrix - Leetcode 74.py --- .../Search a 2D Matrix - Leetcode 74.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py b/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py index d72163f..3a62522 100644 --- a/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py +++ b/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py @@ -19,17 +19,17 @@ def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: r = t - 1 while l <= r: - m = (l + r) // 2 - i = m // n - j = m % n + mid = (l + r) // 2 + i = mid // n + j = mid % n mid_num = matrix[i][j] if target == mid_num: return True elif target < mid_num: - r = m - 1 + r = mid - 1 else: - l = m + 1 + l = mid + 1 return False From ed2bb6f084b5610d22a7cc337b82ee3b84db612f Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Fri, 15 Nov 2024 15:58:00 -0500 Subject: [PATCH 17/55] Update Search a 2D Matrix - Leetcode 74.py --- .../Search a 2D Matrix - Leetcode 74.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py b/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py index 3a62522..8ba178f 100644 --- a/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py +++ b/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py @@ -20,9 +20,9 @@ def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: while l <= r: mid = (l + r) // 2 - i = mid // n - j = mid % n - mid_num = matrix[i][j] + mid_i = mid // n + mid_j = mid % n + mid_num = matrix[mid_i][mid_j] if target == mid_num: return True From fa0cc314b56ec7e842509cb6f2f6ea9ca2eacf6e Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Fri, 15 Nov 2024 16:45:18 -0500 Subject: [PATCH 18/55] Update Find Minimum in Rotated Sorted Array - Leetcode 153.py --- ...inimum in Rotated Sorted Array - Leetcode 153.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Find Minimum in Rotated Sorted Array - Leetcode 153/Find Minimum in Rotated Sorted Array - Leetcode 153.py b/Find Minimum in Rotated Sorted Array - Leetcode 153/Find Minimum in Rotated Sorted Array - Leetcode 153.py index eb84c44..d95496d 100644 --- a/Find Minimum in Rotated Sorted Array - Leetcode 153/Find Minimum in Rotated Sorted Array - Leetcode 153.py +++ b/Find Minimum in Rotated Sorted Array - Leetcode 153/Find Minimum in Rotated Sorted Array - Leetcode 153.py @@ -1,3 +1,16 @@ +# Brute Force Solution +class Solution: + def findMin(self, nums: List[int]) -> int: + minn = float('inf') + for num in nums: + if num < minn: + minn = num + return minn + +# Time: O(n) +# Space: O(1) + +# Optimal Solution class Solution: def findMin(self, nums: List[int]) -> int: n = len(nums) From 298bebcee5da15bcc476d1ae248a665626305aca Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Fri, 22 Nov 2024 15:46:09 -0500 Subject: [PATCH 19/55] Update Invert Binary Tree - Leetcode 226.py --- .../Invert Binary Tree - Leetcode 226.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Invert Binary Tree - Leetcode 226/Invert Binary Tree - Leetcode 226.py b/Invert Binary Tree - Leetcode 226/Invert Binary Tree - Leetcode 226.py index a6ea203..1cc0217 100644 --- a/Invert Binary Tree - Leetcode 226/Invert Binary Tree - Leetcode 226.py +++ b/Invert Binary Tree - Leetcode 226/Invert Binary Tree - Leetcode 226.py @@ -1,3 +1,4 @@ +# YouTube Solution class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: @@ -13,3 +14,21 @@ def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: # Time Complexity: O(n) # Space Complexity: O(h) { here "h" is the height of the tree } + + +# Solution for Bootcamp +class Solution: + def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + def invert(node): + if not node: + return + + node.left, node.right = node.right, node.left + invert(node.left) + invert(node.right) + + invert(root) + return root + +# Time Complexity: O(n) +# Space Complexity: O(h) { here "h" is the height of the tree } From e8388545e853dca8795bbac7b56a77dec9c981fa Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Mon, 25 Nov 2024 09:24:57 -0500 Subject: [PATCH 20/55] Update Same Binary Tree - Leetcode 100.py --- .../Same Binary Tree - Leetcode 100.py | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/Same Binary Tree - Leetcode 100/Same Binary Tree - Leetcode 100.py b/Same Binary Tree - Leetcode 100/Same Binary Tree - Leetcode 100.py index 920eff1..4f12737 100644 --- a/Same Binary Tree - Leetcode 100/Same Binary Tree - Leetcode 100.py +++ b/Same Binary Tree - Leetcode 100/Same Binary Tree - Leetcode 100.py @@ -1,19 +1,25 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: - - def balanced(p, q): - if not p and not q: - return True - - if (p and not q) or (q and not p): - return False - - if p.val != q.val: - return False - - return balanced(p.left, q.left) and balanced(p.right, q.right) - - return balanced(p, q) + # 1: Both Null + if not p and not q: + return True + + # 2: One is Null + if (p and not q) or (q and not p): + return False + + # 3. Values Mismatch + if p.val != q.val: + return False + + return self.isSameTree(p.left, q.left) and \ + self.isSameTree(p.right, q.right) # Time Complexity: O(n) # Space Complexity: O(h) { here "h" is the height of the tree } From c74d7105bb03d5146e496c316dd390ce1d9d873d Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Mon, 25 Nov 2024 09:42:43 -0500 Subject: [PATCH 21/55] Update Same Binary Tree - Leetcode 100.py --- .../Same Binary Tree - Leetcode 100.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Same Binary Tree - Leetcode 100/Same Binary Tree - Leetcode 100.py b/Same Binary Tree - Leetcode 100/Same Binary Tree - Leetcode 100.py index 4f12737..da4e7fc 100644 --- a/Same Binary Tree - Leetcode 100/Same Binary Tree - Leetcode 100.py +++ b/Same Binary Tree - Leetcode 100/Same Binary Tree - Leetcode 100.py @@ -21,5 +21,6 @@ def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: return self.isSameTree(p.left, q.left) and \ self.isSameTree(p.right, q.right) -# Time Complexity: O(n) -# Space Complexity: O(h) { here "h" is the height of the tree } +# Time Complexity: O(n + m) +# Space Complexity: O(n + m) +# m is number of nodes in p, n is number of nodes in Q. From 4a9e4f71d4b075fa7fdde18f31ff520e300a2347 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Mon, 25 Nov 2024 12:13:23 -0500 Subject: [PATCH 22/55] Update Average of Levels in Binary Tree - Leetcode 637.py --- .../Average of Levels in Binary Tree - Leetcode 637.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Average of Levels in Binary Tree - Leetcode 637/Average of Levels in Binary Tree - Leetcode 637.py b/Average of Levels in Binary Tree - Leetcode 637/Average of Levels in Binary Tree - Leetcode 637.py index 91fd272..a8cceec 100644 --- a/Average of Levels in Binary Tree - Leetcode 637/Average of Levels in Binary Tree - Leetcode 637.py +++ b/Average of Levels in Binary Tree - Leetcode 637/Average of Levels in Binary Tree - Leetcode 637.py @@ -12,19 +12,18 @@ def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]: q.append(root) while q: - avg = 0 + summ = 0 n = len(q) for _ in range(n): node = q.popleft() - avg += node.val + summ += node.val if node.left: q.append(node.left) if node.right: q.append(node.right) - avg /= n - avgs.append(avg) + avgs.append(summ / n) return avgs # Time: O(n) From a9323178ea013fb90cc3a91a893a4f0aa0c343cf Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Mon, 25 Nov 2024 13:59:27 -0500 Subject: [PATCH 23/55] Update Validate Binary Search Tree - Leetcode 98.py --- ...lidate Binary Search Tree - Leetcode 98.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Validate Binary Search Tree - Leetcode 98/Validate Binary Search Tree - Leetcode 98.py b/Validate Binary Search Tree - Leetcode 98/Validate Binary Search Tree - Leetcode 98.py index f91cc2a..d8a9a1f 100644 --- a/Validate Binary Search Tree - Leetcode 98/Validate Binary Search Tree - Leetcode 98.py +++ b/Validate Binary Search Tree - Leetcode 98/Validate Binary Search Tree - Leetcode 98.py @@ -13,3 +13,25 @@ def is_valid(node, minn, maxx): # Time Complexity: O(n) # Space Complexity: O(h) { here "h" is height of tree } + + +# Bootcamp solution +class Solution: + def isValidBST(self, root: Optional[TreeNode]) -> bool: + stk = [(root, float('-inf'), float('inf'))] + + while stk: + node, minn, maxx = stk.pop() + + if node.val <= minn or node.val >= maxx: + return False + else: + if node.left: + stk.append((node.left, minn, node.val)) + + if node.right: + stk.append((node.right, node.val, maxx)) + + return True +# Time Complexity: O(n) +# Space Complexity: O(h) { here "h" is height of tree } From 49c875056dc39ebedff897d3e23cf7004b435bf8 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Mon, 25 Nov 2024 15:35:59 -0500 Subject: [PATCH 24/55] Update Last Stone Weight - Leetcode 1046.py --- .../Last Stone Weight - Leetcode 1046.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Last Stone Weight - Leetcode 1046/Last Stone Weight - Leetcode 1046.py b/Last Stone Weight - Leetcode 1046/Last Stone Weight - Leetcode 1046.py index 7459bbe..f0966b5 100644 --- a/Last Stone Weight - Leetcode 1046/Last Stone Weight - Leetcode 1046.py +++ b/Last Stone Weight - Leetcode 1046/Last Stone Weight - Leetcode 1046.py @@ -1,3 +1,22 @@ +# Brute Force Solution +class Solution: + def lastStoneWeight(self, stones: List[int]) -> int: + + def remove_largest(): + index_of_largest = stones.index(max(stones)) + return stones.pop(index_of_largest) + + while len(stones) > 1: + stone_1 = remove_largest() + stone_2 = remove_largest() + if stone_1 != stone_2: + stones.append(stone_1 - stone_2) + + return stones[0] if stones else 0 +# Time: O(n^2) +# Space: O(1) + +# Optimal Solution class Solution: def lastStoneWeight(self, stones: List[int]) -> int: for i in range(len(stones)): From 82ca8c97eae0d6f35c4acde5064deea2f1ba2968 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Mon, 25 Nov 2024 16:09:15 -0500 Subject: [PATCH 25/55] Update Kth Largest Element in an Array - Leetcode 215.py --- ...Kth Largest Element in an Array - Leetcode 215.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Kth Largest Element in an Array - Leetcode 215/Kth Largest Element in an Array - Leetcode 215.py b/Kth Largest Element in an Array - Leetcode 215/Kth Largest Element in an Array - Leetcode 215.py index 888be45..66afa92 100644 --- a/Kth Largest Element in an Array - Leetcode 215/Kth Largest Element in an Array - Leetcode 215.py +++ b/Kth Largest Element in an Array - Leetcode 215/Kth Largest Element in an Array - Leetcode 215.py @@ -1,3 +1,13 @@ +# Brute Force Solution +import heapq +class Solution: + def findKthLargest(self, nums: List[int], k: int) -> int: + nums.sort() + return nums[-k] + +# Time: O(n log n) +# Space: O(1) + import heapq class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: @@ -10,7 +20,7 @@ def findKthLargest(self, nums: List[int], k: int) -> int: heapq.heappop(nums) return -heapq.heappop(nums) - # Max Heap of size n + # Max Heap of size n # Time: O(n + k log n) # Space: O(1) From ca42d8ce5d4dcf2bcf83090b1449ba893313634f Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Tue, 26 Nov 2024 15:37:04 -0500 Subject: [PATCH 26/55] Update Permutations - Leetcode 46.py --- Permutations - Leetcode 46/Permutations - Leetcode 46.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Permutations - Leetcode 46/Permutations - Leetcode 46.py b/Permutations - Leetcode 46/Permutations - Leetcode 46.py index b0483ef..b6209e8 100644 --- a/Permutations - Leetcode 46/Permutations - Leetcode 46.py +++ b/Permutations - Leetcode 46/Permutations - Leetcode 46.py @@ -1,11 +1,11 @@ class Solution: def permute(self, nums: List[int]) -> List[List[int]]: n = len(nums) - ans, sol = [], [] + res, sol = [], [] def backtrack(): if len(sol) == n: - ans.append(sol[:]) + res.append(sol[:]) return for x in nums: @@ -15,7 +15,7 @@ def backtrack(): sol.pop() backtrack() - return ans + return res # Time Complexity: O(n!) # Space Complexity: O(n) From fa01d414995a6eac008fdb3f4db1002a030b7a66 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Tue, 26 Nov 2024 15:37:22 -0500 Subject: [PATCH 27/55] Update Permutations - Leetcode 46.py --- Permutations - Leetcode 46/Permutations - Leetcode 46.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Permutations - Leetcode 46/Permutations - Leetcode 46.py b/Permutations - Leetcode 46/Permutations - Leetcode 46.py index b6209e8..b0483ef 100644 --- a/Permutations - Leetcode 46/Permutations - Leetcode 46.py +++ b/Permutations - Leetcode 46/Permutations - Leetcode 46.py @@ -1,11 +1,11 @@ class Solution: def permute(self, nums: List[int]) -> List[List[int]]: n = len(nums) - res, sol = [], [] + ans, sol = [], [] def backtrack(): if len(sol) == n: - res.append(sol[:]) + ans.append(sol[:]) return for x in nums: @@ -15,7 +15,7 @@ def backtrack(): sol.pop() backtrack() - return res + return ans # Time Complexity: O(n!) # Space Complexity: O(n) From d150d7fd3851cdabfa60d69e37a037b68af44398 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Tue, 26 Nov 2024 15:37:43 -0500 Subject: [PATCH 28/55] Update Subsets - Leetcode 78.py --- Subsets - Leetcode 78/Subsets - Leetcode 78.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Subsets - Leetcode 78/Subsets - Leetcode 78.py b/Subsets - Leetcode 78/Subsets - Leetcode 78.py index 0739377..0c77de4 100644 --- a/Subsets - Leetcode 78/Subsets - Leetcode 78.py +++ b/Subsets - Leetcode 78/Subsets - Leetcode 78.py @@ -1,11 +1,11 @@ class Solution: def subsets(self, nums: List[int]) -> List[List[int]]: n = len(nums) - res, sol = [], [] + ans, sol = [], [] def backtrack(i): if i == n: - res.append(sol[:]) + ans.append(sol[:]) return # Don't pick nums[i] @@ -17,7 +17,7 @@ def backtrack(i): sol.pop() backtrack(0) - return res + return ans # Time Complexity: O(2^n) # Space Complexity: O(n) From 088d2e3ddebdfd9212c746c0f87d1ed352675f36 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Tue, 7 Jan 2025 14:33:53 -0500 Subject: [PATCH 29/55] Update Contains Duplicate - Leetcode 217.py --- .../Contains Duplicate - Leetcode 217.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py b/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py index 9026a1b..30feb67 100644 --- a/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py +++ b/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py @@ -11,6 +11,19 @@ def containsDuplicate(self, nums: List[int]) -> bool: # Time Complexity: O(n^2) # Space Complexity: O(1) +# Better Brute Force Solution +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + n = len(nums) + + for i in range(n): + for j in range(i+1, n): + if nums[i] == nums[j]: + return True + return False + # Time Complexity: O(n^2) + # Space Complexity: O(1) + # Optimal Solution class Solution: def containsDuplicate(self, nums: list[int]) -> bool: From 6dab4048fe5b9abd17ba235569cf8b35ce3047fc Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Tue, 7 Jan 2025 14:39:50 -0500 Subject: [PATCH 30/55] Update Contains Duplicate - Leetcode 217.py --- .../Contains Duplicate - Leetcode 217.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py b/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py index 30feb67..2d506f9 100644 --- a/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py +++ b/Contains Duplicate - Leetcode 217/Contains Duplicate - Leetcode 217.py @@ -27,12 +27,12 @@ def containsDuplicate(self, nums: List[int]) -> bool: # Optimal Solution class Solution: def containsDuplicate(self, nums: list[int]) -> bool: - h = set() + s = set() for num in nums: - if num in h: + if num in s: return True else: - h.add(num) + s.add(num) return False # Time Complexity: O(n) From 3b61bc7d179356169173e0dba6fb6882d4de0e60 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 9 Jan 2025 11:09:08 -0500 Subject: [PATCH 31/55] Update Best Time to Buy and Sell Stock - Leetcode 121.py --- ...me to Buy and Sell Stock - Leetcode 121.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Best Time to Buy and Sell Stock - Leetcode 121/Best Time to Buy and Sell Stock - Leetcode 121.py b/Best Time to Buy and Sell Stock - Leetcode 121/Best Time to Buy and Sell Stock - Leetcode 121.py index 6415e77..1d391c9 100644 --- a/Best Time to Buy and Sell Stock - Leetcode 121/Best Time to Buy and Sell Stock - Leetcode 121.py +++ b/Best Time to Buy and Sell Stock - Leetcode 121/Best Time to Buy and Sell Stock - Leetcode 121.py @@ -31,3 +31,23 @@ def maxProfit(self, prices: List[int]) -> int: max_profit = profit return max_profit + + +# Optimal Solution for Bootcamp +class Solution: + def maxProfit(self, prices: List[int]) -> int: + # Time: O(n) + # Space: O(1) + min_price = float('inf') + max_profit = 0 + + for price in prices: + profit = price - min_price + + if price < min_price: + min_price = price + + if profit > max_profit: + max_profit = profit + + return max_profit From bfef12438243f449b3d9133481858a504329f09e Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 9 Jan 2025 11:10:55 -0500 Subject: [PATCH 32/55] Update Best Time to Buy and Sell Stock - Leetcode 121.py --- .../Best Time to Buy and Sell Stock - Leetcode 121.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Best Time to Buy and Sell Stock - Leetcode 121/Best Time to Buy and Sell Stock - Leetcode 121.py b/Best Time to Buy and Sell Stock - Leetcode 121/Best Time to Buy and Sell Stock - Leetcode 121.py index 1d391c9..faf4100 100644 --- a/Best Time to Buy and Sell Stock - Leetcode 121/Best Time to Buy and Sell Stock - Leetcode 121.py +++ b/Best Time to Buy and Sell Stock - Leetcode 121/Best Time to Buy and Sell Stock - Leetcode 121.py @@ -44,10 +44,7 @@ def maxProfit(self, prices: List[int]) -> int: for price in prices: profit = price - min_price - if price < min_price: - min_price = price - - if profit > max_profit: - max_profit = profit + min_price = min(price, min_price) + max_profit = max(profit, max_profit) return max_profit From 74e6b5626a788660868ea7aa073fce863cf02830 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Mon, 13 Jan 2025 11:48:55 -0500 Subject: [PATCH 33/55] Update Two Sum - Leetcode 1.py --- Two Sum - Leetcode 1/Two Sum - Leetcode 1.py | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Two Sum - Leetcode 1/Two Sum - Leetcode 1.py b/Two Sum - Leetcode 1/Two Sum - Leetcode 1.py index 620d24a..cb8b63a 100644 --- a/Two Sum - Leetcode 1/Two Sum - Leetcode 1.py +++ b/Two Sum - Leetcode 1/Two Sum - Leetcode 1.py @@ -1,3 +1,26 @@ +# Brute Force Solution +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + n = len(nums) + for i in range(n): + for j in range(n): + if nums[i] + nums[j] == target and i != j: + return (i, j) + # Time: O(n^2) + # Space: O(1) + +# Better Brute Force Solution +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + n = len(nums) + for i in range(n): + for j in range(i+1, n): + if nums[i] + nums[j] == target: + return (i, j) + # Time: O(n^2) + # Space: O(1) + +# 2-Pass Optimal Solution class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: h = {} @@ -9,6 +32,19 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: if y in h and h[y] != i: return [i, h[y]] +# Time Complexity: O(n) +# Space Complexity: O(n) +# One-Pass Optimal Solution (For Bootcamp) +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + h = {} + n = len(nums) + for i, x in enumerate(nums): + y = target - nums[i] + if y in h: + return [i, h[y]] + else: + h[x] = i # Time Complexity: O(n) # Space Complexity: O(n) From 99d51ddd1e05802214de93a72d83ec4e79265505 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Mon, 13 Jan 2025 14:08:59 -0500 Subject: [PATCH 34/55] Update Two Sum - Leetcode 1.py --- Two Sum - Leetcode 1/Two Sum - Leetcode 1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Two Sum - Leetcode 1/Two Sum - Leetcode 1.py b/Two Sum - Leetcode 1/Two Sum - Leetcode 1.py index cb8b63a..7b51c94 100644 --- a/Two Sum - Leetcode 1/Two Sum - Leetcode 1.py +++ b/Two Sum - Leetcode 1/Two Sum - Leetcode 1.py @@ -41,7 +41,7 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: h = {} n = len(nums) for i, x in enumerate(nums): - y = target - nums[i] + y = target - x if y in h: return [i, h[y]] else: From f536db8e77734715ae04888a71b4ca55ac2d3c6e Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Wed, 15 Jan 2025 11:52:30 -0500 Subject: [PATCH 35/55] Update Reverse String - Leetcode 344.py --- .../Reverse String - Leetcode 344.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Reverse String - Leetcode 344/Reverse String - Leetcode 344.py b/Reverse String - Leetcode 344/Reverse String - Leetcode 344.py index 48cd942..0f947f0 100644 --- a/Reverse String - Leetcode 344/Reverse String - Leetcode 344.py +++ b/Reverse String - Leetcode 344/Reverse String - Leetcode 344.py @@ -1,3 +1,21 @@ +# Brute Force Solution +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + n = len(s) + T = [] + for i in range(n-1, -1, -1): + T.append(s[i]) + + for i in range(n): + s[i] = T[i] + + # Time: O(n) + # Space: O(n) + +# Two Pointers (Optimal) Solution class Solution: def reverseString(self, s: List[str]) -> None: """ From 6a45c13197d563da98f1b224a0c028f2ba9465af Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Wed, 15 Jan 2025 15:06:29 -0500 Subject: [PATCH 36/55] Update Squares of a Sorted Array - Leetcode 977.py --- ...quares of a Sorted Array - Leetcode 977.py | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py b/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py index ed1c9bd..2ed626c 100644 --- a/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py +++ b/Squares of a Sorted Array - Leetcode 977/Squares of a Sorted Array - Leetcode 977.py @@ -12,8 +12,33 @@ def sortedSquares(self, nums: List[int]) -> List[int]: # Time: O(n log n) # Space: O(1) +# Optimal Solution for Bootcamp +class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + n = len(nums) + L, R = 0, n-1 + result = [0] * n -# Optimal Solution + for i in range(n): + nums[i] = nums[i] ** 2 + + j = n-1 + while L <= R: + if nums[L] > nums[R]: + result[j] = nums[L] + L += 1 + else: + result[j] = nums[R] + R -= 1 + + j -= 1 + + return result +# Time: O(n) +# Space: O(n) + + +# Optimal Solution in YT Video class Solution: def sortedSquares(self, nums: List[int]) -> List[int]: left = 0 @@ -34,26 +59,3 @@ def sortedSquares(self, nums: List[int]) -> List[int]: # Time Complexity: O(n) # Space Complexity: O(n) - -# Optimal Solution for Bootcamp -class Solution: - def sortedSquares(self, nums: List[int]) -> List[int]: - n = len(nums) - L, R = 0, n-1 - result = [] - - for i in range(n): - nums[i] = nums[i] ** 2 - - while L <= R: - if nums[L] > nums[R]: - result.append(nums[L]) - L += 1 - else: - result.append(nums[R]) - R -= 1 - - result.reverse() - return result -# Time: O(n) -# Space: O(n) From 1e97beabfc0c68bc73252dfcce81b64185ffcbb0 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Fri, 17 Jan 2025 17:12:02 -0500 Subject: [PATCH 37/55] Update Search a 2D Matrix - Leetcode 74.py --- .../Search a 2D Matrix - Leetcode 74.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py b/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py index 8ba178f..18a6f3d 100644 --- a/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py +++ b/Search a 2D Matrix - Leetcode 74/Search a 2D Matrix - Leetcode 74.py @@ -9,6 +9,19 @@ def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: # Time: O(m * n) # Space: O(1) +# Brute Force Solution With (i, j) indices +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + m, n = len(matrix), len(matrix[0]) + for i in range(m): + for j in range(n): + if target == matrix[i][j]: + return True + + return False +# Time: O(m * n) +# Space: O(1) + # Optimal Solution class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: From 6ce7911a69be5d9bbad1555c5c932db2bba6a317 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Sat, 25 Jan 2025 11:22:20 -0500 Subject: [PATCH 38/55] Update Binary Search - Leetcode 704.py --- .../Binary Search - Leetcode 704.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Binary Search - Leetcode 704/Binary Search - Leetcode 704.py b/Binary Search - Leetcode 704/Binary Search - Leetcode 704.py index 12a81b5..ad5c032 100644 --- a/Binary Search - Leetcode 704/Binary Search - Leetcode 704.py +++ b/Binary Search - Leetcode 704/Binary Search - Leetcode 704.py @@ -1,3 +1,14 @@ +# Brute Force Solution +class Solution: + def search(self, nums: List[int], target: int) -> int: + n = len(nums) + for i in range(n): + if nums[i] == target: + return i + return -1 + # Time: O(n) + # Space: O(1) + class Solution: def search(self, nums: List[int], target: int) -> int: left = 0 From 79365631876dacc1db6464c24ce332f6dfb15078 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Sat, 25 Jan 2025 11:44:12 -0500 Subject: [PATCH 39/55] Update First Bad Version - Leetcode 278.py --- .../First Bad Version - Leetcode 278.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/First Bad Version - Leetcode 278/First Bad Version - Leetcode 278.py b/First Bad Version - Leetcode 278/First Bad Version - Leetcode 278.py index a215336..c6b9559 100644 --- a/First Bad Version - Leetcode 278/First Bad Version - Leetcode 278.py +++ b/First Bad Version - Leetcode 278/First Bad Version - Leetcode 278.py @@ -1,6 +1,15 @@ +# Brute Force Solution +class Solution: + def firstBadVersion(self, n: int) -> int: + for version in range(1, n+1): + if isBadVersion(version): + return version + # Time: O(n) + # Space: O(1) + +# Optimal Solution # The isBadVersion API is already defined for you. # def isBadVersion(version: int) -> bool: - class Solution: def firstBadVersion(self, n: int) -> int: L = 1 @@ -13,4 +22,6 @@ def firstBadVersion(self, n: int) -> int: else: L = M + 1 - return L # Time: O(Log n), Space: O(1) + return L + # Time: O(Log n) + # Space: O(1) From 80cab14c21514fef991961dabd7ca90f4eba5283 Mon Sep 17 00:00:00 2001 From: jianxun-p Date: Mon, 27 Jan 2025 14:10:48 -0500 Subject: [PATCH 40/55] added: brute force soln for Max Consecutive Ones III --- .../brute force.cpp | 29 ++++++++++++++++++ .../brute force.java | 27 +++++++++++++++++ .../brute force.js | 30 +++++++++++++++++++ .../brute force.py | 19 ++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 Max Consecutive Ones III - Leetcode 1004/brute force.cpp create mode 100644 Max Consecutive Ones III - Leetcode 1004/brute force.java create mode 100644 Max Consecutive Ones III - Leetcode 1004/brute force.js create mode 100644 Max Consecutive Ones III - Leetcode 1004/brute force.py diff --git a/Max Consecutive Ones III - Leetcode 1004/brute force.cpp b/Max Consecutive Ones III - Leetcode 1004/brute force.cpp new file mode 100644 index 0000000..3a8c72c --- /dev/null +++ b/Max Consecutive Ones III - Leetcode 1004/brute force.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int longestOnes(vector& nums, int k) { + int n = nums.size(); + int maxLen = 0; + + // Iterate over all starting points of subarrays + for (int i = 0; i < n; ++i) { + + // Check each subarray starting from `i` + for (int j = i; j < n; ++j) { + int zeroCount = 0; + for (int c = i; c <= j; ++c) { + if (nums[c] == 0) + zeroCount++; + } + + // If the number of zeroes exceeds k, + // update the maximum length + if (zeroCount <= k) { + maxLen = max(maxLen, j - i + 1); + } + + } + } + + return maxLen; + } +}; diff --git a/Max Consecutive Ones III - Leetcode 1004/brute force.java b/Max Consecutive Ones III - Leetcode 1004/brute force.java new file mode 100644 index 0000000..1df7076 --- /dev/null +++ b/Max Consecutive Ones III - Leetcode 1004/brute force.java @@ -0,0 +1,27 @@ +public class Solution { + public int longestOnes(int[] nums, int k) { + int maxLength = 0; + + // Iterate over all starting points + for (int i = 0; i < nums.length; i++) { + + // Iterate over all possible subarrays starting from i + for (int j = i; j < nums.length; j++) { + + int zeroCount = 0; + for (int c = i; c <= j; ++c) { + if (nums[c] == 0) + ++zeroCount; + } + + // Update maxLength if this subarray is valid + if (zeroCount <= k) { + maxLength = Math.max(maxLength, j - i + 1); + } + + } + } + + return maxLength; + } +} diff --git a/Max Consecutive Ones III - Leetcode 1004/brute force.js b/Max Consecutive Ones III - Leetcode 1004/brute force.js new file mode 100644 index 0000000..cd743b2 --- /dev/null +++ b/Max Consecutive Ones III - Leetcode 1004/brute force.js @@ -0,0 +1,30 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var longestOnes = function(nums, k) { + let maxLength = 0; + + // Iterate over all starting points + for (let i = 0; i < nums.length; i++) { + + // Iterate over all possible subarrays starting from i + for (let j = i; j < nums.length; j++) { + let zeroCount = 0; + for (let c = i; c <= j; ++c) { + if (nums[c] == 0) + ++zeroCount; + } + + // If the number of zeroes exceeds k, + // update the maximum length + if (zeroCount <= k) { + maxLength = Math.max(maxLength, j - i + 1); + } + + } + } + + return maxLength; +}; diff --git a/Max Consecutive Ones III - Leetcode 1004/brute force.py b/Max Consecutive Ones III - Leetcode 1004/brute force.py new file mode 100644 index 0000000..be6b73d --- /dev/null +++ b/Max Consecutive Ones III - Leetcode 1004/brute force.py @@ -0,0 +1,19 @@ +class Solution(object): + def longestOnes(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: int + """ + longest_ones = 0 + + # Iterate over all starting points + for i in range(len(nums)): + + # Iterate over all possible subarrays starting from i + for j in range(i + 1, len(nums) + 1): + window_len = j - i + num_zeros = nums[i:j].count(0) # O(k) operation + if num_zeros <= k: + longest_ones = max(longest_ones, window_len) + return longest_ones From 0b04aa228b7ec6e4125cc448d7de65c1c9057414 Mon Sep 17 00:00:00 2001 From: jianxun-p Date: Mon, 27 Jan 2025 14:48:02 -0500 Subject: [PATCH 41/55] added: brute force soln for Maximum Average Subarray I --- .../brute force.cpp | 22 +++++++++++++++++++ .../brute force.java | 22 +++++++++++++++++++ .../brute force.js | 22 +++++++++++++++++++ .../brute force.py | 20 +++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 Maximum Average Subarray I - Leetcode 643/brute force.cpp create mode 100644 Maximum Average Subarray I - Leetcode 643/brute force.java create mode 100644 Maximum Average Subarray I - Leetcode 643/brute force.js create mode 100644 Maximum Average Subarray I - Leetcode 643/brute force.py diff --git a/Maximum Average Subarray I - Leetcode 643/brute force.cpp b/Maximum Average Subarray I - Leetcode 643/brute force.cpp new file mode 100644 index 0000000..e399fe5 --- /dev/null +++ b/Maximum Average Subarray I - Leetcode 643/brute force.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + double findMaxAverage(const vector& nums, int k) { + double max_avg = 0; + + // setting max_avg to any constant numeric value + // here is incorrect!!! + // So initialize it with some values from the input + for (int i = 0; i < k; ++i) + max_avg += nums[i]; + max_avg /= k; + + for (int i = 0; i <= nums.size() - k; ++i) { + double avg = 0; + for (int j = i; j < i + k; ++j) + avg += nums[j]; + avg /= k; + max_avg = max(max_avg, avg); + } + return max_avg; + } +}; diff --git a/Maximum Average Subarray I - Leetcode 643/brute force.java b/Maximum Average Subarray I - Leetcode 643/brute force.java new file mode 100644 index 0000000..6dfd656 --- /dev/null +++ b/Maximum Average Subarray I - Leetcode 643/brute force.java @@ -0,0 +1,22 @@ +class Solution { + public double findMaxAverage(int[] nums, int k) { + double max_avg = 0; + + // setting max_avg to any constant numeric value + // here is incorrect!!! + // So initialize it with some values from the input + for (int i = 0; i < k; ++i) + max_avg += nums[i]; + max_avg /= k; + + for (int i = 0; i <= nums.length - k; ++i) { + double avg = 0; + for (int j = i; j < i + k; ++j) + avg += nums[j]; + avg /= k; + max_avg = Math.max(max_avg, avg); + } + return max_avg; + + } +} diff --git a/Maximum Average Subarray I - Leetcode 643/brute force.js b/Maximum Average Subarray I - Leetcode 643/brute force.js new file mode 100644 index 0000000..07469e7 --- /dev/null +++ b/Maximum Average Subarray I - Leetcode 643/brute force.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var findMaxAverage = function(nums, k) { + const avg = (vals, start, len) => { + let sum = 0; + for (let i = start; i < start + len; i++) { + sum += vals[i]; + } + return sum / len; + }; + + // Calculate initial maximum average + let maxAvg = avg(nums, 0, k); + for (let i = 0; i <= nums.length - k; i++) { + maxAvg = Math.max(maxAvg, avg(nums, i, k)); + } + + return maxAvg; +}; diff --git a/Maximum Average Subarray I - Leetcode 643/brute force.py b/Maximum Average Subarray I - Leetcode 643/brute force.py new file mode 100644 index 0000000..1de4fbf --- /dev/null +++ b/Maximum Average Subarray I - Leetcode 643/brute force.py @@ -0,0 +1,20 @@ +class Solution(object): + def findMaxAverage(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: float + """ + + def avg(vals): + return sum(vals) / float(len(vals)) + + # Note, setting max_avg to any constant numeric value + # here is incorrect!!! + # Say 0, if nums = [-1, -2, -3, -4], k = 2, + # the output would be incorrect + max_avg = avg(nums[:k]) + for i in range(len(nums) - k + 1): + max_avg = max(max_avg, avg(nums[i:i+k])) # avg is O(k) + return max_avg + From aa923e81ec1eb532122e58f3b846b8dd3515fed7 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Tue, 28 Jan 2025 16:39:58 -0500 Subject: [PATCH 42/55] Update Valid Anagram - Leetcode 242.py --- Valid Anagram - Leetcode 242/Valid Anagram - Leetcode 242.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Valid Anagram - Leetcode 242/Valid Anagram - Leetcode 242.py b/Valid Anagram - Leetcode 242/Valid Anagram - Leetcode 242.py index f50edef..96fcb99 100644 --- a/Valid Anagram - Leetcode 242/Valid Anagram - Leetcode 242.py +++ b/Valid Anagram - Leetcode 242/Valid Anagram - Leetcode 242.py @@ -9,5 +9,5 @@ def isAnagram(self, s: str, t: str) -> bool: return s_dict == t_dict -# Time complexity: O(n) -# Space complexity: O(n) +# Time complexity: O(S + T) +# Space complexity: O(S + T) From 8070c4647541af6e246873ff6ca4d0acd65ad49a Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Tue, 28 Jan 2025 16:40:56 -0500 Subject: [PATCH 43/55] Update Valid Anagram - Leetcode 242.py --- Valid Anagram - Leetcode 242/Valid Anagram - Leetcode 242.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Valid Anagram - Leetcode 242/Valid Anagram - Leetcode 242.py b/Valid Anagram - Leetcode 242/Valid Anagram - Leetcode 242.py index 96fcb99..3d85457 100644 --- a/Valid Anagram - Leetcode 242/Valid Anagram - Leetcode 242.py +++ b/Valid Anagram - Leetcode 242/Valid Anagram - Leetcode 242.py @@ -9,5 +9,6 @@ def isAnagram(self, s: str, t: str) -> bool: return s_dict == t_dict -# Time complexity: O(S + T) -# Space complexity: O(S + T) +# Let n be the length of the longest word +# Time complexity: O(n) +# Space complexity: O(n) From e8253692f5389ca880c728a52bc3d07cd96eb7d4 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Wed, 29 Jan 2025 14:51:29 -0500 Subject: [PATCH 44/55] Update Merge Intervals - Leetcode 56.py --- .../Merge Intervals - Leetcode 56.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Merge Intervals - Leetcode 56/Merge Intervals - Leetcode 56.py b/Merge Intervals - Leetcode 56/Merge Intervals - Leetcode 56.py index f98cfb7..b94f8ac 100644 --- a/Merge Intervals - Leetcode 56/Merge Intervals - Leetcode 56.py +++ b/Merge Intervals - Leetcode 56/Merge Intervals - Leetcode 56.py @@ -12,3 +12,23 @@ def merge(self, intervals: List[List[int]]) -> List[List[int]]: return merged # Time: O(n log n) # Space: O(n) + + +class Solution: + def merge(self, intervals: List[List[int]]) -> List[List[int]]: + intervals.sort(key=lambda x: x[0]) # Sort by start time + i = 0 # Index to track merged intervals + n = len(intervals) + + for j in range(1, n): + # Check if the current interval overlaps with the previous one + if intervals[i][1] >= intervals[j][0]: + intervals[i][1] = max(intervals[i][1], intervals[j][1]) # Merge + else: + i += 1 + intervals[i] = intervals[j] # Move next non-overlapping interval + + return intervals[:i+1] # Return the relevant merged portion + + # Time: O(n log n) + # Space: O(1) From 6228ccc72545313dedf8000c77179f115475a6f3 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Wed, 29 Jan 2025 16:45:52 -0500 Subject: [PATCH 45/55] Update Longest Substring Without Repeating Characters - Leetcode 3.py --- ...ng Without Repeating Characters - Leetcode 3.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.py b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.py index ee06243..52010ff 100644 --- a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.py +++ b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.py @@ -1,3 +1,17 @@ +# Brute Force +def length_of_longest_substring(s: str) -> int: + max_length = 0 + + for i in range(len(s)): + for j in range(i, len(s)): + if s[j] in s[i:j]: # Check if the character already appeared in the substring + break + max_length = max(max_length, j - i + 1) + + return max_length + # Time: O(n^3) + # Space: O(n) + class Solution: def lengthOfLongestSubstring(self, s: str) -> int: l = 0 From 0d10b40ac8409d8bd450e232e502c7127bd723d2 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Wed, 5 Feb 2025 10:15:00 -0500 Subject: [PATCH 46/55] add question --- ...e Level Order Traversal - Leetcode 429.cpp | 30 ++++++++++++++++++ ... Level Order Traversal - Leetcode 429.java | 26 ++++++++++++++++ ...ee Level Order Traversal - Leetcode 429.js | 20 ++++++++++++ ...ee Level Order Traversal - Leetcode 429.py | 31 +++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.cpp create mode 100644 N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.java create mode 100644 N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.js create mode 100644 N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.py diff --git a/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.cpp b/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.cpp new file mode 100644 index 0000000..accc47c --- /dev/null +++ b/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +class Solution { +public: + vector> levelOrder(Node* root) { + vector> output; + if (!root) return output; + + queue q; + q.push(root); + + while (!q.empty()) { + int n = q.size(); + vector level; + for (int i = 0; i < n; i++) { + Node* node = q.front(); + q.pop(); + level.push_back(node->val); + for (Node* child : node->children) { + q.push(child); + } + } + output.push_back(level); + } + return output; + } +}; diff --git a/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.java b/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.java new file mode 100644 index 0000000..2759a8f --- /dev/null +++ b/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.java @@ -0,0 +1,26 @@ +import java.util.*; + + +class Solution { + public List> levelOrder(Node root) { + List> output = new ArrayList<>(); + if (root == null) return output; + + Queue q = new LinkedList<>(); + q.add(root); + + while (!q.isEmpty()) { + int n = q.size(); + List level = new ArrayList<>(); + for (int i = 0; i < n; i++) { + Node node = q.poll(); + level.add(node.val); + if (node.children != null) { + q.addAll(node.children); + } + } + output.add(level); + } + return output; + } +} diff --git a/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.js b/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.js new file mode 100644 index 0000000..5b8adf9 --- /dev/null +++ b/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.js @@ -0,0 +1,20 @@ +var levelOrder = function(root) { + if (!root) return []; + + let output = []; + let q = [root]; + + while (q.length > 0) { + let n = q.length; + let level = []; + for (let i = 0; i < n; i++) { + let node = q.shift(); + level.push(node.val); + if (node.children) { + q.push(...node.children); + } + } + output.push(level); + } + return output; +}; diff --git a/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.py b/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.py new file mode 100644 index 0000000..34fa64d --- /dev/null +++ b/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.py @@ -0,0 +1,31 @@ +""" +# Definition for a Node. +class Node: + def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None): + self.val = val + self.children = children +""" + +class Solution: + def levelOrder(self, root: 'Node') -> List[List[int]]: + if not root: + return [] + output = [] + q = deque() + q.append(root) + + while q: + n = len(q) + level = [] + for _ in range(n): + node = q.popleft() + level.append(node.val) + for child in node.children: + q.append(child) + output.append(level) + + return output + + # Time: O(n) + # Space: O(n) + # n is number of nodes in the tree \ No newline at end of file From dc139842896e6f08eae0d035912bff38df485c6c Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Wed, 5 Feb 2025 10:24:09 -0500 Subject: [PATCH 47/55] Add question --- ...e Level Order Traversal - Leetcode 429.cpp | 0 ... Level Order Traversal - Leetcode 429.java | 0 ...ee Level Order Traversal - Leetcode 429.js | 0 ...ee Level Order Traversal - Leetcode 429.py | 0 ...Tree Preorder Traversal - Leetcode 429.cpp | 30 +++++++++++++++++++ ...ree Preorder Traversal - Leetcode 429.java | 18 +++++++++++ ... Tree Preorder Traversal - Leetcode 429.js | 15 ++++++++++ ... Tree Preorder Traversal - Leetcode 429.py | 25 ++++++++++++++++ 8 files changed, 88 insertions(+) rename {N-ary Tree Level Order Traversal => N-ary Tree Level Order Traversal - Leetcode 429}/N-ary Tree Level Order Traversal - Leetcode 429.cpp (100%) rename {N-ary Tree Level Order Traversal => N-ary Tree Level Order Traversal - Leetcode 429}/N-ary Tree Level Order Traversal - Leetcode 429.java (100%) rename {N-ary Tree Level Order Traversal => N-ary Tree Level Order Traversal - Leetcode 429}/N-ary Tree Level Order Traversal - Leetcode 429.js (100%) rename {N-ary Tree Level Order Traversal => N-ary Tree Level Order Traversal - Leetcode 429}/N-ary Tree Level Order Traversal - Leetcode 429.py (100%) create mode 100644 N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.cpp create mode 100644 N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.java create mode 100644 N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.js create mode 100644 N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.py diff --git a/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.cpp b/N-ary Tree Level Order Traversal - Leetcode 429/N-ary Tree Level Order Traversal - Leetcode 429.cpp similarity index 100% rename from N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.cpp rename to N-ary Tree Level Order Traversal - Leetcode 429/N-ary Tree Level Order Traversal - Leetcode 429.cpp diff --git a/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.java b/N-ary Tree Level Order Traversal - Leetcode 429/N-ary Tree Level Order Traversal - Leetcode 429.java similarity index 100% rename from N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.java rename to N-ary Tree Level Order Traversal - Leetcode 429/N-ary Tree Level Order Traversal - Leetcode 429.java diff --git a/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.js b/N-ary Tree Level Order Traversal - Leetcode 429/N-ary Tree Level Order Traversal - Leetcode 429.js similarity index 100% rename from N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.js rename to N-ary Tree Level Order Traversal - Leetcode 429/N-ary Tree Level Order Traversal - Leetcode 429.js diff --git a/N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.py b/N-ary Tree Level Order Traversal - Leetcode 429/N-ary Tree Level Order Traversal - Leetcode 429.py similarity index 100% rename from N-ary Tree Level Order Traversal/N-ary Tree Level Order Traversal - Leetcode 429.py rename to N-ary Tree Level Order Traversal - Leetcode 429/N-ary Tree Level Order Traversal - Leetcode 429.py diff --git a/N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.cpp b/N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.cpp new file mode 100644 index 0000000..accc47c --- /dev/null +++ b/N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +class Solution { +public: + vector> levelOrder(Node* root) { + vector> output; + if (!root) return output; + + queue q; + q.push(root); + + while (!q.empty()) { + int n = q.size(); + vector level; + for (int i = 0; i < n; i++) { + Node* node = q.front(); + q.pop(); + level.push_back(node->val); + for (Node* child : node->children) { + q.push(child); + } + } + output.push_back(level); + } + return output; + } +}; diff --git a/N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.java b/N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.java new file mode 100644 index 0000000..ac21f6d --- /dev/null +++ b/N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.java @@ -0,0 +1,18 @@ +import java.util.*; + + +class Solution { + public List preorder(Node root) { + List output = new ArrayList<>(); + if (root == null) return output; + dfs(root, output); + return output; + } + + private void dfs(Node node, List output) { + output.add(node.val); + for (Node child : node.children) { + dfs(child, output); + } + } +} diff --git a/N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.js b/N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.js new file mode 100644 index 0000000..b3ae283 --- /dev/null +++ b/N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.js @@ -0,0 +1,15 @@ +var preorder = function(root) { + if (!root) return []; + + let output = []; + + function dfs(node) { + output.push(node.val); + for (let child of node.children) { + dfs(child); + } + } + + dfs(root); + return output; +}; diff --git a/N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.py b/N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.py new file mode 100644 index 0000000..896cfa2 --- /dev/null +++ b/N-ary Tree Preorder Traversal - Leetcode 589/N-ary Tree Preorder Traversal - Leetcode 429.py @@ -0,0 +1,25 @@ +""" +# Definition for a Node. +class Node: + def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None): + self.val = val + self.children = children +""" + +class Solution: + def preorder(self, root: 'Node') -> List[int]: + if not root: + return [] + + output = [] + def dfs(node): + output.append(node.val) + for child in node.children: + dfs(child) + + dfs(root) + return output + + # Time: O(n) + # Space: O(n) + # n is number of nodes in the tree \ No newline at end of file From 42d52a1c1843861abc0d944c0ec8acf4394a425a Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Fri, 7 Feb 2025 10:51:04 -0500 Subject: [PATCH 48/55] Update Maximum Depth of Binary Tree - Leetcode 104.py --- ...mum Depth of Binary Tree - Leetcode 104.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Maximum Depth of Binary Tree - Leetcode 104/Maximum Depth of Binary Tree - Leetcode 104.py b/Maximum Depth of Binary Tree - Leetcode 104/Maximum Depth of Binary Tree - Leetcode 104.py index 061c10f..9cf4d38 100644 --- a/Maximum Depth of Binary Tree - Leetcode 104/Maximum Depth of Binary Tree - Leetcode 104.py +++ b/Maximum Depth of Binary Tree - Leetcode 104/Maximum Depth of Binary Tree - Leetcode 104.py @@ -1,3 +1,4 @@ +# DFS class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: if not root: @@ -10,3 +11,28 @@ def maxDepth(self, root: Optional[TreeNode]) -> int: # Time Complexity: O(n) # Space Complexity: O(h) { here "h" is the height of the binary tree } + +# BFS +class Solution: + def maxDepth(self, root): + if not root: + return 0 # Height of an empty tree is 0 + + queue = deque([root]) + height = 0 + + while queue: + level_size = len(queue) # Number of nodes at the current level + + for _ in range(level_size): + node = queue.popleft() + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + + height += 1 # Increment height at each level + + return height + # Time Complexity: O(n) + # Space Complexity: O(n) From 96a6a66681f3e2289de3869626b874b13b13f043 Mon Sep 17 00:00:00 2001 From: jianxun-p <160386302+jianxun-p@users.noreply.github.com> Date: Tue, 11 Feb 2025 11:40:12 -0500 Subject: [PATCH 49/55] Added Brute Force Solution (#31) * added: brute force soln for Longest Substring Without Repeating Characters * Added brute force solution for Coin Change * Updated Longest Substring Without Repeating Characters --- .../Coin Change - Leetcode 322.cpp | 24 +++++++++++++++++ .../Coin Change - Leetcode 322.java | 23 ++++++++++++++++ .../Coin Change - Leetcode 322.js | 23 ++++++++++++++++ .../Coin Change - Leetcode 322.py | 19 ++++++++++++++ ...hout Repeating Characters - Leetcode 3.cpp | 24 +++++++++++++++++ ...out Repeating Characters - Leetcode 3.java | 26 +++++++++++++++++++ ...thout Repeating Characters - Leetcode 3.js | 23 ++++++++++++++++ 7 files changed, 162 insertions(+) diff --git a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.cpp b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.cpp index 32eb939..0482c5d 100644 --- a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.cpp +++ b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.cpp @@ -2,6 +2,30 @@ #include using namespace std; + +/** + * Brute force approach with simple recursion + */ +class Solution { +public: + int coinChange(vector& coins, int amount) { + if (amount == 0) + return 0; + else if (amount < 0) + return -1; + + int min_cnt = -1; + for (int coin : coins) { + int cnt = coinChange(coins, amount - coin); + if (cnt >= 0) + min_cnt = min_cnt < 0 ? cnt + 1 : min(min_cnt, cnt + 1); + } + return min_cnt; + } +}; + + + class Solution { public: int coinChange(vector& coins, int amount) { diff --git a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.java b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.java index c7d1ee4..59c5ade 100644 --- a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.java +++ b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.java @@ -1,5 +1,28 @@ import java.util.Arrays; + +/** + * Brute force approach with simple recursion + */ +class Solution { + public int coinChange(int[] coins, int amount) { + if (amount == 0) + return 0; + else if (amount < 0) + return -1; + + int min_cnt = -1; + for (int coin : coins) { + int cnt = coinChange(coins, amount - coin); + if (cnt >= 0) + min_cnt = min_cnt < 0 ? cnt + 1 : Math.min(min_cnt, cnt + 1); + } + return min_cnt; + } +} + + + public class Solution { public int coinChange(int[] coins, int amount) { int[] dp = new int[amount + 1]; diff --git a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.js b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.js index 14fd04c..e6aee29 100644 --- a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.js +++ b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.js @@ -1,3 +1,26 @@ +/** + * Brute force approach with simple recursion + * + * @param {number[]} coins + * @param {number} amount + * @return {number} + */ +var coinChange = function(coins, amount) { + if (amount == 0) + return 0; + else if (amount < 0) + return -1; + + let min_cnt = -1; + for (let coin of coins) { + let cnt = coinChange(coins, amount - coin); + if (cnt >= 0) + min_cnt = min_cnt < 0 ? cnt + 1 : Math.min(min_cnt, cnt + 1); + } + return min_cnt; +}; + + /** * @param {number[]} coins * @param {number} amount diff --git a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.py b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.py index c35592d..6d88a3e 100644 --- a/Coin Change - Leetcode 322/Coin Change - Leetcode 322.py +++ b/Coin Change - Leetcode 322/Coin Change - Leetcode 322.py @@ -1,3 +1,22 @@ +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + # Brute force with simple recursion + # Time: O(Coins ^ Amount) + # Space: O(Amount) + if amount == 0: + return 0 + elif amount < 0: + return -1 + + min_cnt = -1 + for coin in coins: + cnt = self.coinChange(coins, amount - coin) + if cnt >= 0: + min_cnt = cnt + 1 if min_cnt < 0 else min(min_cnt, cnt + 1) + return min_cnt + + + class Solution: def coinChange(self, coins: List[int], amount: int) -> int: # Top Down DP (Memoization) diff --git a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.cpp b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.cpp index 250c098..1932b34 100644 --- a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.cpp +++ b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.cpp @@ -2,6 +2,30 @@ #include using namespace std; + + +class Solution { +public: + int lengthOfLongestSubstring(const string s) { + + int longest = 0; + + for (auto i = 0; i < s.size(); ++i) { + for (auto substr_len = 1; i + substr_len <= s.size(); ++substr_len) { + unordered_set seen{s.cbegin() + i, s.cbegin() + i + substr_len}; + if (seen.size() == substr_len) + longest = max(longest, substr_len); + } + } + + return longest; + } +}; +// Time Complexity: O(n^3) +// Space Complexity: O(n) + + + class Solution { public: int lengthOfLongestSubstring(string s) { diff --git a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.java b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.java index 510219a..1fdaaac 100644 --- a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.java +++ b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.java @@ -1,5 +1,31 @@ import java.util.HashSet; + +// brute force +// Time Complexity: O(n^3) +// Space Complexity: O(n) +public class Solution { + public int lengthOfLongestSubstring(String s) { + int longest = 0; + int n = s.length(); + for (int i = 0; i < n; i++) { + for (int substr_len = 1; i + substr_len <= n; ++substr_len) { + HashSet seen = new HashSet<>(); + for (int j = i; j < i + substr_len; ++j) + seen.add(s.charAt(j)); + + if (seen.size() == substr_len) + longest = Math.max(longest, substr_len); + } + } + + return longest; + } +} + + + + public class Solution { public int lengthOfLongestSubstring(String s) { HashSet set = new HashSet<>(); diff --git a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.js b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.js index 807da86..499fccf 100644 --- a/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.js +++ b/Longest Substring Without Repeating Characters - Leetcode 3/Longest Substring Without Repeating Characters - Leetcode 3.js @@ -1,3 +1,26 @@ +// brute force +// Time Complexity: O(n^3) +// Space Complexity: O(n) +var lengthOfLongestSubstring = function(s) { + + let longest = 0; + let n = s.length; + for (let i = 0; i < n; ++i) { + for (let substr_len = 1; i + substr_len <= n; ++substr_len) { + var seen = new Set(); + for (let j = i; j < i + substr_len; ++j) { + seen.add(s[j]); + } + if (seen.size == substr_len) { + longest = Math.max(longest, substr_len); + } + } + } + + return longest; +}; + + var lengthOfLongestSubstring = function(s) { const set = new Set(); let l = 0, longest = 0; From 274b0a2c73850369136f93f43d42a613bc430092 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Wed, 12 Feb 2025 20:27:57 -0500 Subject: [PATCH 50/55] Update Kth Largest Element in an Array - Leetcode 215.py --- .../Kth Largest Element in an Array - Leetcode 215.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Kth Largest Element in an Array - Leetcode 215/Kth Largest Element in an Array - Leetcode 215.py b/Kth Largest Element in an Array - Leetcode 215/Kth Largest Element in an Array - Leetcode 215.py index 66afa92..31354d6 100644 --- a/Kth Largest Element in an Array - Leetcode 215/Kth Largest Element in an Array - Leetcode 215.py +++ b/Kth Largest Element in an Array - Leetcode 215/Kth Largest Element in an Array - Leetcode 215.py @@ -1,5 +1,4 @@ # Brute Force Solution -import heapq class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: nums.sort() From 708329f643dea6829f776303a140135441ac2d8a Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Wed, 12 Feb 2025 20:46:24 -0500 Subject: [PATCH 51/55] Update Top K Frequent Elements - Leetcode 347.py --- .../Top K Frequent Elements - Leetcode 347.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Top K Frequent Elements - Leetcode 347/Top K Frequent Elements - Leetcode 347.py b/Top K Frequent Elements - Leetcode 347/Top K Frequent Elements - Leetcode 347.py index f399730..a89a7e6 100644 --- a/Top K Frequent Elements - Leetcode 347/Top K Frequent Elements - Leetcode 347.py +++ b/Top K Frequent Elements - Leetcode 347/Top K Frequent Elements - Leetcode 347.py @@ -16,6 +16,22 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: # Time: O(n log k), Space: O(k) +import heapq +from collections import Counter + +def top_k_frequent(nums, k): + # Step 1: Count frequency + freq_map = Counter(nums) + + # Step 2: Use max heap (invert frequency to simulate max heap) + max_heap = [(-freq, num) for num, freq in freq_map.items()] + heapq.heapify(max_heap) # Convert list into a heap in O(n) + + # Step 3: Extract k most frequent elements + result = [heapq.heappop(max_heap)[1] for _ in range(k)] + return result + # Max Heap + # Buckets from collections import Counter class Solution: From 87711b96c50db46385c3f6f4fce5ef372ed0c965 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Sun, 16 Feb 2025 21:49:01 -0500 Subject: [PATCH 52/55] Update Linked List Cycle - Leetcode 141.py --- .../Linked List Cycle - Leetcode 141.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 From 9f071de0ca4519dfe90a9df54928b8078c0c46c4 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 20 Feb 2025 15:59:19 -0500 Subject: [PATCH 53/55] Update Rotting Oranges - Leetcode 994.py --- .../Rotting Oranges - Leetcode 994.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Rotting Oranges - Leetcode 994/Rotting Oranges - Leetcode 994.py b/Rotting Oranges - Leetcode 994/Rotting Oranges - Leetcode 994.py index 32f3ce0..54ec593 100644 --- a/Rotting Oranges - Leetcode 994/Rotting Oranges - Leetcode 994.py +++ b/Rotting Oranges - Leetcode 994/Rotting Oranges - Leetcode 994.py @@ -1,3 +1,43 @@ +# 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 + + # Directions for adjacent cells (right, down, left, up) + Directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] + + # 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: From 0a2d746baf1a4d69fdefd308454ff61378df26d0 Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 20 Feb 2025 20:24:08 -0500 Subject: [PATCH 54/55] Update Course Schedule II - Leetcode 210.py --- .../Course Schedule II - Leetcode 210.py | 1 - 1 file changed, 1 deletion(-) 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: From fcc230cffaf190305568fda1a5ed40976c137ffa Mon Sep 17 00:00:00 2001 From: Gregory Hogg Date: Thu, 20 Feb 2025 20:58:36 -0500 Subject: [PATCH 55/55] Update Rotting Oranges - Leetcode 994.py --- .../Rotting Oranges - Leetcode 994.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/Rotting Oranges - Leetcode 994/Rotting Oranges - Leetcode 994.py b/Rotting Oranges - Leetcode 994/Rotting Oranges - Leetcode 994.py index 54ec593..c14eb3f 100644 --- a/Rotting Oranges - Leetcode 994/Rotting Oranges - Leetcode 994.py +++ b/Rotting Oranges - Leetcode 994/Rotting Oranges - Leetcode 994.py @@ -17,9 +17,6 @@ def orangesRotting(self, grid): elif grid[i][j] == 1: FreshCount += 1 - # Directions for adjacent cells (right, down, left, up) - Directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] - # Perform BFS while Q and FreshCount > 0: NumRotting = len(Q)