From 868e017ad9cdd0070820f7e8c10c44ba63f4a6b6 Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sun, 6 Apr 2025 16:46:19 +0900 Subject: [PATCH 01/16] init solution files --- contains-duplicate/mandoolala.py | 0 house-robber/mandoolala.py | 0 longest-consecutive-sequence/mandoolala.py | 0 top-k-frequent-elements/mandoolala.py | 0 two-sum/mandoolala.py | 0 5 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 contains-duplicate/mandoolala.py create mode 100644 house-robber/mandoolala.py create mode 100644 longest-consecutive-sequence/mandoolala.py create mode 100644 top-k-frequent-elements/mandoolala.py create mode 100644 two-sum/mandoolala.py diff --git a/contains-duplicate/mandoolala.py b/contains-duplicate/mandoolala.py new file mode 100644 index 000000000..e69de29bb diff --git a/house-robber/mandoolala.py b/house-robber/mandoolala.py new file mode 100644 index 000000000..e69de29bb diff --git a/longest-consecutive-sequence/mandoolala.py b/longest-consecutive-sequence/mandoolala.py new file mode 100644 index 000000000..e69de29bb diff --git a/top-k-frequent-elements/mandoolala.py b/top-k-frequent-elements/mandoolala.py new file mode 100644 index 000000000..e69de29bb diff --git a/two-sum/mandoolala.py b/two-sum/mandoolala.py new file mode 100644 index 000000000..e69de29bb From 5e4c93e4dff583abeea1e088f5c3250ce204a391 Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sun, 6 Apr 2025 18:11:45 +0900 Subject: [PATCH 02/16] contains duplicate sol --- contains-duplicate/mandoolala.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/contains-duplicate/mandoolala.py b/contains-duplicate/mandoolala.py index e69de29bb..24df5b5ff 100644 --- a/contains-duplicate/mandoolala.py +++ b/contains-duplicate/mandoolala.py @@ -0,0 +1,23 @@ +""" +https://leetcode.com/problems/contains-duplicate/ +""" +from typing import List + +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + ''' + [Complexity] + Time: O(n) + Space: O(n) + ''' + return (len(nums) != len(set(nums))) + + ''' + alternative: + nums_set = set() + for num in nums: + if num in nums_set: + return True + nums_set.add(num) + return False + ''' \ No newline at end of file From 4d40b13c940866fb9c88899e6e71074c3e2e51fb Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sun, 6 Apr 2025 18:14:40 +0900 Subject: [PATCH 03/16] two sum sol --- contains-duplicate/mandoolala.py | 3 ++- two-sum/mandoolala.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/contains-duplicate/mandoolala.py b/contains-duplicate/mandoolala.py index 24df5b5ff..76d732f99 100644 --- a/contains-duplicate/mandoolala.py +++ b/contains-duplicate/mandoolala.py @@ -20,4 +20,5 @@ def containsDuplicate(self, nums: List[int]) -> bool: return True nums_set.add(num) return False - ''' \ No newline at end of file + ''' + \ No newline at end of file diff --git a/two-sum/mandoolala.py b/two-sum/mandoolala.py index e69de29bb..81fa801b1 100644 --- a/two-sum/mandoolala.py +++ b/two-sum/mandoolala.py @@ -0,0 +1,27 @@ +from typing import List + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + # return indices of two numbers such that they add up to target + ''' + [Complexity] + Time: O(n) + Space: O(1) + ''' + nums_dict = {} + for idx, num in enumerate(nums): + remaining = target - num + if remaining in nums_dict: + return [idx, nums_dict[remaining]] + nums_dict[num] = idx + ''' + [Complexity] + Time: O(n^2) + Space: O(1) + + for i in range(0,len(nums)-1): + for j in range(i+1, len(nums)): + sum = nums[i] + nums[j] + if sum == target: + return [i, j] + ''' From 5ceaf52232aabc4a07f131a086d4e5e755b6db71 Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sun, 6 Apr 2025 18:16:23 +0900 Subject: [PATCH 04/16] eol --- contains-duplicate/mandoolala.py | 1 - 1 file changed, 1 deletion(-) diff --git a/contains-duplicate/mandoolala.py b/contains-duplicate/mandoolala.py index 76d732f99..ff90b6981 100644 --- a/contains-duplicate/mandoolala.py +++ b/contains-duplicate/mandoolala.py @@ -21,4 +21,3 @@ def containsDuplicate(self, nums: List[int]) -> bool: nums_set.add(num) return False ''' - \ No newline at end of file From a309b1e331845fee63b3a4af3070f0f5d5ddb72a Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sun, 13 Apr 2025 21:50:38 +0900 Subject: [PATCH 05/16] top k elements --- top-k-frequent-elements/mandoolala.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/top-k-frequent-elements/mandoolala.py b/top-k-frequent-elements/mandoolala.py index e69de29bb..ee9dcab19 100644 --- a/top-k-frequent-elements/mandoolala.py +++ b/top-k-frequent-elements/mandoolala.py @@ -0,0 +1,15 @@ +from typing import List + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + countDict = {} + for num in nums: + if num in countDict: + countDict[num] += 1 + else: + countDict[num] = 1 + sortedDictList = sorted(countDict.items(), key=lambda item: item[1], reverse = True) + freqElements = [] + for i in range(k): + freqElements.append(sortedDictList[i][0]) + return freqElements \ No newline at end of file From 4048621f1db2987885801f76fac221115f45a6a3 Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sun, 13 Apr 2025 21:54:26 +0900 Subject: [PATCH 06/16] eol --- top-k-frequent-elements/mandoolala.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/top-k-frequent-elements/mandoolala.py b/top-k-frequent-elements/mandoolala.py index ee9dcab19..3ed56f7a1 100644 --- a/top-k-frequent-elements/mandoolala.py +++ b/top-k-frequent-elements/mandoolala.py @@ -1,5 +1,6 @@ from typing import List + class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: countDict = {} @@ -8,8 +9,8 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: countDict[num] += 1 else: countDict[num] = 1 - sortedDictList = sorted(countDict.items(), key=lambda item: item[1], reverse = True) + sortedDictList = sorted(countDict.items(), key=lambda item: item[1], reverse=True) freqElements = [] for i in range(k): freqElements.append(sortedDictList[i][0]) - return freqElements \ No newline at end of file + return freqElements From 085e2978547524c25622a890c9c08fdeb12f7e0c Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sat, 19 Apr 2025 16:25:24 +0900 Subject: [PATCH 07/16] try using heap solution --- top-k-frequent-elements/mandoolala.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/top-k-frequent-elements/mandoolala.py b/top-k-frequent-elements/mandoolala.py index ee9dcab19..eeec52726 100644 --- a/top-k-frequent-elements/mandoolala.py +++ b/top-k-frequent-elements/mandoolala.py @@ -2,6 +2,29 @@ class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: + ''' + Using (min)heap + [Complexity] + Time: O(log n) + Space: O(n+k) + ''' + from heapq import heappush, heappop + counter = {} + for num in nums: + counter[num] = counter.get(num, 0) + 1 + heap = [] + for num, freq in counter.items(): + heappush(heap, (freq, num)) + if len(heap) > k: + heappop(heap) + return [num for _, num in heap] + ''' + Using hash table + [Complexity] + Time: O(n log n) + Space: O(n) + ''' + ''' countDict = {} for num in nums: if num in countDict: @@ -12,4 +35,5 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: freqElements = [] for i in range(k): freqElements.append(sortedDictList[i][0]) - return freqElements \ No newline at end of file + return freqElements + ''' From 90318a22b3e01b28c37114ec4c324f78db78c63d Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sat, 19 Apr 2025 16:51:11 +0900 Subject: [PATCH 08/16] longest consecutive sequence --- longest-consecutive-sequence/mandoolala.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/longest-consecutive-sequence/mandoolala.py b/longest-consecutive-sequence/mandoolala.py index e69de29bb..8840b4445 100644 --- a/longest-consecutive-sequence/mandoolala.py +++ b/longest-consecutive-sequence/mandoolala.py @@ -0,0 +1,16 @@ +from typing import List + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + num_set = set(nums) + longest = 0 + + for num in nums: + if num - 1 not in num_set: + cnt = 1 + next_num = num + 1 + while next_num in num_set: + cnt += 1 + next_num += 1 + longest = max(longest, cnt) + return longest From a907534540df7d4511fb57efeb3fc9ba1b625387 Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sat, 19 Apr 2025 17:09:56 +0900 Subject: [PATCH 09/16] use num set for time limit exceeded --- longest-consecutive-sequence/mandoolala.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/longest-consecutive-sequence/mandoolala.py b/longest-consecutive-sequence/mandoolala.py index 8840b4445..47fcee207 100644 --- a/longest-consecutive-sequence/mandoolala.py +++ b/longest-consecutive-sequence/mandoolala.py @@ -4,13 +4,11 @@ class Solution: def longestConsecutive(self, nums: List[int]) -> int: num_set = set(nums) longest = 0 - - for num in nums: - if num - 1 not in num_set: - cnt = 1 - next_num = num + 1 - while next_num in num_set: - cnt += 1 - next_num += 1 - longest = max(longest, cnt) + for num in num_set: + if num - 1 in num_set: + continue + length = 1 + while num + length in num_set: + length += 1 + longest = max(length, longest) return longest From b9cd6b62f0b8b9cd4b5541387475280cb82961bc Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sat, 19 Apr 2025 18:19:55 +0900 Subject: [PATCH 10/16] house robber --- house-robber/mandoolala.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/house-robber/mandoolala.py b/house-robber/mandoolala.py index e69de29bb..86120f611 100644 --- a/house-robber/mandoolala.py +++ b/house-robber/mandoolala.py @@ -0,0 +1,25 @@ +from typing import List + +class Solution: + def rob(self, nums: List[int]) -> int: + ''' + [Complexity] + Time: O(n) + Space: O(n) + ''' + cnt = len(nums) + + if cnt == 1: + return nums[0] + if cnt == 2: + return max(nums[0], nums[1]) + + dp = [0] * cnt + dp[0] = nums[0] + dp[1] = max(nums[0], nums[1]) + + for i in range(2, cnt): + # skip: dp[i-1] + # rob: dp[i-2]+nums[i] + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]) + return max(dp) From ea008fb68b282edabdff1f688d485de3766a975c Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sat, 19 Apr 2025 18:29:36 +0900 Subject: [PATCH 11/16] init sol files --- 3sum/mandoolala.py | 0 climbing-stairs/mandoolala.py | 0 product-of-array-except-self/mandoolala.py | 0 valid-anagram/mandoolala.py | 0 validate-binary-search-tree/mandoolala.py | 0 5 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 3sum/mandoolala.py create mode 100644 climbing-stairs/mandoolala.py create mode 100644 product-of-array-except-self/mandoolala.py create mode 100644 valid-anagram/mandoolala.py create mode 100644 validate-binary-search-tree/mandoolala.py diff --git a/3sum/mandoolala.py b/3sum/mandoolala.py new file mode 100644 index 000000000..e69de29bb diff --git a/climbing-stairs/mandoolala.py b/climbing-stairs/mandoolala.py new file mode 100644 index 000000000..e69de29bb diff --git a/product-of-array-except-self/mandoolala.py b/product-of-array-except-self/mandoolala.py new file mode 100644 index 000000000..e69de29bb diff --git a/valid-anagram/mandoolala.py b/valid-anagram/mandoolala.py new file mode 100644 index 000000000..e69de29bb diff --git a/validate-binary-search-tree/mandoolala.py b/validate-binary-search-tree/mandoolala.py new file mode 100644 index 000000000..e69de29bb From 219acf40a3ab1939102b0e0e2560aa1ff2549fa4 Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sun, 20 Apr 2025 15:20:46 +0900 Subject: [PATCH 12/16] valid anagram #218 --- valid-anagram/mandoolala.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/valid-anagram/mandoolala.py b/valid-anagram/mandoolala.py index e69de29bb..0411cf292 100644 --- a/valid-anagram/mandoolala.py +++ b/valid-anagram/mandoolala.py @@ -0,0 +1,3 @@ +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + return sorted(s) == sorted(t) From 817593729921f1f8a6f235dcd9c76eee91ab1ed3 Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sun, 20 Apr 2025 15:43:29 +0900 Subject: [PATCH 13/16] Climbing Stairs #230 --- climbing-stairs/mandoolala.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/climbing-stairs/mandoolala.py b/climbing-stairs/mandoolala.py index e69de29bb..91f390a12 100644 --- a/climbing-stairs/mandoolala.py +++ b/climbing-stairs/mandoolala.py @@ -0,0 +1,12 @@ +class Solution: + def climbStairs(self, n: int) -> int: + if n == 1: + return 1 + if n == 2: + return 2 + dp = [0]*n + dp[0] = 1 + dp[1] = 2 + for i in range(2, n): + dp[i] = dp[i-1] + dp[i-2] + return dp[n-1] From 915c79c8f02a42dfb5323a2758d36d075d218286 Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sun, 20 Apr 2025 16:29:37 +0900 Subject: [PATCH 14/16] Product of Array Except Self #239 --- product-of-array-except-self/mandoolala.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/product-of-array-except-self/mandoolala.py b/product-of-array-except-self/mandoolala.py index e69de29bb..05c0e3595 100644 --- a/product-of-array-except-self/mandoolala.py +++ b/product-of-array-except-self/mandoolala.py @@ -0,0 +1,14 @@ +from typing import List + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + answer = [1] * len(nums) + left_product = 1 + for i in range(len(nums) - 1): + left_product *= nums[i] + answer[i + 1] *= left_product + right_product = 1 + for i in range(len(nums) - 1, 0, -1): + right_product *= nums[i] + answer[i - 1] *= right_product + return answer From ff4aa6def277c5a31fb044b5c731c3b7e8c4b433 Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sun, 20 Apr 2025 17:27:30 +0900 Subject: [PATCH 15/16] 3Sum #241 --- 3sum/mandoolala.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/3sum/mandoolala.py b/3sum/mandoolala.py index e69de29bb..6410b01ef 100644 --- a/3sum/mandoolala.py +++ b/3sum/mandoolala.py @@ -0,0 +1,21 @@ +from typing import List + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + answer = set() + sorted_nums = sorted(nums) + + for i in range(len(nums) - 2): + low, high = i + 1, len(nums) - 1 + while low < high: + three_sum = sorted_nums[i] + sorted_nums[low] + sorted_nums[high] + if three_sum == 0: + answer.add((sorted_nums[i], sorted_nums[low], sorted_nums[high])) + low += 1 + high -= 1 + elif three_sum < 0: + low += 1 + elif three_sum > 0: + high -= 1 + return list(answer) + From 8bd1ce86ae083016a6bb8a0163521ff5cab7d958 Mon Sep 17 00:00:00 2001 From: MJ Kang Date: Sun, 20 Apr 2025 17:43:49 +0900 Subject: [PATCH 16/16] Validate Binary Search Tree #251 --- validate-binary-search-tree/mandoolala.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/validate-binary-search-tree/mandoolala.py b/validate-binary-search-tree/mandoolala.py index e69de29bb..c3a70c739 100644 --- a/validate-binary-search-tree/mandoolala.py +++ b/validate-binary-search-tree/mandoolala.py @@ -0,0 +1,20 @@ +from typing import Optional + +# 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 isValidBST(self, root: Optional[TreeNode]) -> bool: + def traverse(node, low, high): + if not node: + return True + if not (low < node.val < high): + return False + return traverse(node.left, low, node.val) and traverse(node.right, node.val, high) + + return traverse(root, float("-inf"), float("inf"))