From fe64669afa8479a04c6c8b8bdde7d78bc02935eb Mon Sep 17 00:00:00 2001 From: sun912 Date: Tue, 27 Aug 2024 21:36:10 +0900 Subject: [PATCH 1/6] two-sum solution --- two-sum/sun912.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 two-sum/sun912.py diff --git a/two-sum/sun912.py b/two-sum/sun912.py new file mode 100644 index 000000000..e69de29bb From 5740518ce73c541bccb53a26a5e71779f9521276 Mon Sep 17 00:00:00 2001 From: sun912 Date: Thu, 5 Sep 2024 09:56:23 +0900 Subject: [PATCH 2/6] [W4] valid-palindrome solution --- two-sum/sun912.py | 0 valid-palindrome/sun912.py | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+) delete mode 100644 two-sum/sun912.py create mode 100644 valid-palindrome/sun912.py diff --git a/two-sum/sun912.py b/two-sum/sun912.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/valid-palindrome/sun912.py b/valid-palindrome/sun912.py new file mode 100644 index 000000000..310afcc70 --- /dev/null +++ b/valid-palindrome/sun912.py @@ -0,0 +1,18 @@ +class Solution: + def isPalindrome(self, s: str) -> bool: + str = list(s.lower()) + chars = [] + for c in str: + if (ord(c) >= ord("a") and ord(c) <= ord("z")) or (ord(c) >= ord("0") and ord(c) <= ord("9")): + chars.append(c) + + for i in range(len(chars)//2): + + if len(c)%2 == 0: + if chars[i] != chars[-(i+1)]: + return False + else: + if chars[i] != chars[-(i+1)]: + return False + + return True From 76ce972ee7c7e810c9fdcdc2c99ef63c83df640b Mon Sep 17 00:00:00 2001 From: sun912 Date: Thu, 5 Sep 2024 21:14:19 +0900 Subject: [PATCH 3/6] [W4] missing number solution --- missing-number/sun912.py | 12 ++++++++++++ valid-palindrome/sun912.py | 4 ++++ 2 files changed, 16 insertions(+) create mode 100644 missing-number/sun912.py diff --git a/missing-number/sun912.py b/missing-number/sun912.py new file mode 100644 index 000000000..805e520b1 --- /dev/null +++ b/missing-number/sun912.py @@ -0,0 +1,12 @@ +""" +TC: O(n) +SC: O(n) +""" + +class Solution: + def missingNumber(self, nums: List[int]) -> int: + nums_set = set(nums) + for i in range(len(nums)): + if i not in nums_set: + return i + return len(nums) diff --git a/valid-palindrome/sun912.py b/valid-palindrome/sun912.py index 310afcc70..929857284 100644 --- a/valid-palindrome/sun912.py +++ b/valid-palindrome/sun912.py @@ -1,3 +1,7 @@ +""" +TC: O(n) +SC: O(n) +""" class Solution: def isPalindrome(self, s: str) -> bool: str = list(s.lower()) From 405f6e66fb0fc2c009e5dc7f4079641e9ed9dbd9 Mon Sep 17 00:00:00 2001 From: sun912 Date: Sat, 7 Sep 2024 10:41:19 +0900 Subject: [PATCH 4/6] longest-consecutive-sequence solution --- longest-consecutive-sequence/sun912.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 longest-consecutive-sequence/sun912.py diff --git a/longest-consecutive-sequence/sun912.py b/longest-consecutive-sequence/sun912.py new file mode 100644 index 000000000..a02be4a51 --- /dev/null +++ b/longest-consecutive-sequence/sun912.py @@ -0,0 +1,19 @@ +""" +TC: O(n) +SC: O(n) +""" + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + longest = 0 + num_set = set(nums) + print(num_set) + + for n in num_set: + if (n-1) not in num_set: + length = 1 + while (n+length) in num_set: + length += 1 + longest = max(longest, length) + + return longest From e66691e1448a76c20befee8ae995c8523103385d Mon Sep 17 00:00:00 2001 From: sun912 Date: Sat, 7 Sep 2024 10:54:56 +0900 Subject: [PATCH 5/6] word-search solution --- word-search/sun912.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 word-search/sun912.py diff --git a/word-search/sun912.py b/word-search/sun912.py new file mode 100644 index 000000000..2f36b73dd --- /dev/null +++ b/word-search/sun912.py @@ -0,0 +1,41 @@ +""" +TC: O(ROWS*COLS*4^word length) +SC: O(ROWS*COLS*word length) +""" + +class Solution: + def exist(self, board: List[List[str]], word: str) -> bool: + ROWS = len(board) + COLS = len(board[0]) + path = set() + + def dfs(r,c,i): + if i == len(word): + return True + + if (r < 0 or + c < 0 or + r >= ROWS or + c >= COLS or + word[i] != board[r][c] or + (r,c) in path + ): + return False + + path.add((r,c)) + + res = (dfs(r+1, c, i+1) or + dfs(r, c+1, i+1) or + dfs(r-1, c, i+1) or + dfs(r, c-1, i+1) + ) + path.remove((r,c)) + return res + + for r in range(ROWS): + for c in range(COLS): + if dfs(r,c, 0): return True + + return False + + From a3b4539e6f05ff06c3a83f9c9ce68d1facac7439 Mon Sep 17 00:00:00 2001 From: sun912 Date: Sat, 7 Sep 2024 11:55:02 +0900 Subject: [PATCH 6/6] maximum-product-subarray solution --- maximum-product-subarray/sun912.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 maximum-product-subarray/sun912.py diff --git a/maximum-product-subarray/sun912.py b/maximum-product-subarray/sun912.py new file mode 100644 index 000000000..a557bb95f --- /dev/null +++ b/maximum-product-subarray/sun912.py @@ -0,0 +1,15 @@ +""" + TC: O(n) + SC: O(1) +""" +class Solution: + def maxProduct(self, nums: List[int]) -> int: + minimum, maximum = 1, 1 + result = nums[0] + + for num in nums: + minimum, maximum = min(minimum * num, maximum * num, num), max(minimum * num, maximum * num, num) + result = max(maximum, result) + + return result +