From 43cb051c3d8c9f5699e5b794761fee709ce2fa2b Mon Sep 17 00:00:00 2001 From: jonghun Date: Thu, 19 Dec 2024 00:34:08 +0900 Subject: [PATCH 1/2] add week 3 solution --- 3sum/rivkode.py | 8 ++++++++ climbing-stairs/rivkode.py | 20 ++++++++++++++++++++ valid-anagram/rivkode.py | 26 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 3sum/rivkode.py create mode 100644 climbing-stairs/rivkode.py create mode 100644 valid-anagram/rivkode.py diff --git a/3sum/rivkode.py b/3sum/rivkode.py new file mode 100644 index 000000000..c93384bf9 --- /dev/null +++ b/3sum/rivkode.py @@ -0,0 +1,8 @@ +from typing import List + +# use hashtable + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + result = [] + return result diff --git a/climbing-stairs/rivkode.py b/climbing-stairs/rivkode.py new file mode 100644 index 000000000..9e030f10b --- /dev/null +++ b/climbing-stairs/rivkode.py @@ -0,0 +1,20 @@ +# Time Complexity O(n) +# - traversing for loop takes O(n) +# Space Complexity O(n) +# - appending for loop it takes O(n) + +class Solution: + def climbStairs(self, n: int) -> int: + stage = [1, 2, 3] + + for i in range(3, 45): + value = stage[i - 1] + stage[i - 2] + stage.append(value) + + return stage[n - 1] + +if __name__ == "__main__": + solution = Solution() + result = solution.climbStairs(5) + print(result) + diff --git a/valid-anagram/rivkode.py b/valid-anagram/rivkode.py new file mode 100644 index 000000000..2e44f029a --- /dev/null +++ b/valid-anagram/rivkode.py @@ -0,0 +1,26 @@ +from typing import List + +# Time Complexity O(n log n) +# - when sorting by sorted function(TimSort) for each string it takes O(nlogn) +# - traversing for loop takes O(n) +# Space Complexity O(n) +# - when sorting takes O(n) + +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + s_sorted = sorted(s) + t_sorted = sorted(t) + + for i in range(len(s_sorted)): + if s_sorted[i] != t_sorted[i]: + return False + return True + +if __name__ == "__main__": + solution = Solution() + + s = "nagaram" + t = "anagram" + + result = solution.isAnagram(s, t) + print(result) From f88e7acf227c26e45b044c331dfbc2c2943c3e41 Mon Sep 17 00:00:00 2001 From: jonghun Date: Sat, 21 Dec 2024 19:10:02 +0900 Subject: [PATCH 2/2] add week 3 solution --- 3sum/rivkode.py | 67 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/3sum/rivkode.py b/3sum/rivkode.py index c93384bf9..063ee7d5d 100644 --- a/3sum/rivkode.py +++ b/3sum/rivkode.py @@ -1,8 +1,65 @@ -from typing import List +from typing import List, Tuple, Set + +# threeSum3() +# Time Complexity O(n ^ 2) +# - when sorting by sorted function(TimSort) for each string it takes O(nlogn) +# - traversing for loop takes O(n) and check left and right in the while loop at the same time. +# Space Complexity O(n) +# - when sorting takes O(1) -# use hashtable class Solution: - def threeSum(self, nums: List[int]) -> List[List[int]]: - result = [] - return result + def threeSum(self, nums: List[int]) -> list[tuple[int, ...]]: + + triplets = set() + for i in range(len(nums) - 2): + for j in range(i + 1, len(nums)): + for k in range(j + 1, len(nums)): + if nums[i] + nums[j] + nums[k] == 0: + triplet = [nums[i], nums[j], nums[k]] + triplets.add(tuple(sorted(triplet))) + + return list(triplets) + + def threeSum2(self, nums: List[int]) -> list[tuple[int, ...]]: + triplets = set() + + for i in range(len(nums)): + seen = set() + for j in range(i + 1, len(nums)): + res = -(nums[i] + nums[j]) + if res in seen: + triplet = [nums[i], nums[j], res] + triplets.add(tuple(sorted(triplet))) + seen.add(nums[j]) + + return list(triplets) + + def threeSum3(self, nums: List[int]) -> list(tuple[int, ...]): + triplets = set() + nums.sort() + + for i in range(len(nums) - 2): + l = i + 1 + r = len(nums) - 1 + + while l < r: + res = nums[l] + nums[r] + nums[i] + if res > 0: + r -= 1 + elif res < 0: + l += 1 + elif res == 0: + triple = (nums[l], nums[r], nums[i]) + triplets.add(triple) + l, r = l + 1, r - 1 + else: + raise Exception + return list(triplets) + +if __name__ == "__main__": + nums = [-1,0,1,2,-1,-4] + + + solution = Solution() + solution.threeSum3(nums)