From a32772e8d456973949a7c61be7ea324141dfdf25 Mon Sep 17 00:00:00 2001 From: siroo Date: Sun, 15 Dec 2024 05:44:33 +0900 Subject: [PATCH 1/7] Two sum Solution --- reverse-bits/jungsiroo.py | 9 +++++++++ two-sum/jungsiroo.py | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 reverse-bits/jungsiroo.py create mode 100644 two-sum/jungsiroo.py diff --git a/reverse-bits/jungsiroo.py b/reverse-bits/jungsiroo.py new file mode 100644 index 000000000..e010b64f0 --- /dev/null +++ b/reverse-bits/jungsiroo.py @@ -0,0 +1,9 @@ +class Solution: + def reverseBits(self, n: int) -> int: + # TC : O(32) + # SC : O(32) + + bit_str = ''.join(reversed(format(n, 'b').zfill(32))) + return int(bit_str, 2) + + diff --git a/two-sum/jungsiroo.py b/two-sum/jungsiroo.py new file mode 100644 index 000000000..8a5f7a80f --- /dev/null +++ b/two-sum/jungsiroo.py @@ -0,0 +1,27 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + """ + O(n^2) 보다 빠르게 풀기 위한 방법 : 정렬 후 투포인터 이용 + Tc : O(nlogn) + Sc : O(n) // queue 생성 + """ + n = len(nums) + queue = [] + + for i in range(n): + queue.append([nums[i], i]) + + queue.sort() + + start, end = 0, n-1 + sum_ = queue[start][0] + queue[end][0] + + while sum_ != target: + if sum_ > target: + end -= 1 + else: + start += 1 + sum_ = queue[start][0] + queue[end][0] + + return [queue[start][1], queue[end][1]] + From c1e30f24a90cd18a8925d52eaab01e6533cb653e Mon Sep 17 00:00:00 2001 From: siroo Date: Sun, 15 Dec 2024 06:26:48 +0900 Subject: [PATCH 2/7] Product of Array Except Self Solution --- product-of-array-except-self/jungsiroo.py | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 product-of-array-except-self/jungsiroo.py diff --git a/product-of-array-except-self/jungsiroo.py b/product-of-array-except-self/jungsiroo.py new file mode 100644 index 000000000..d1fe06042 --- /dev/null +++ b/product-of-array-except-self/jungsiroo.py @@ -0,0 +1,46 @@ +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + # Solution 1 :모든 수를 미리 곱해두기 + # 0을 포함한다면 미리 0의 개수를 체크해두기 + + n = len(nums) + ret = [0 for _ in range(n)] + """ + all_product = 1 + non_zero_product = 1 + zero_cnt = 0 + + for x in nums: + all_product *= x + + if x == 0: + zero_cnt += 1 + else: + non_zero_product *= x + + if zero_cnt > 1: + return ret + + for i in range(n): + if nums[i] == 0: + ans = non_zero_product + else: + ans = all_product // nums[i] + ret[i] = ans + return ret + """ + + # Solution 2 : w/o division + # prefix sum과 같이 풀이 진행 + # Tc : O(n) / Sc : O(n) - no extra memory + + prefix_pr, postfix_pr = 1, 1 + for i in range(n): + ret[i] = prefix_pr + prefix_pr *= nums[i] + for i in range(n-1, -1, -1): + ret[i] *= postfix_pr + postfix_pr *= nums[i] + return ret + + From 571311ff198f097de4b903d5949f9c73af7f0773 Mon Sep 17 00:00:00 2001 From: siroo Date: Tue, 17 Dec 2024 17:32:48 +0900 Subject: [PATCH 3/7] Delete Week3 solutions(current : week2) --- product-of-array-except-self/jungsiroo.py | 46 ----------------------- reverse-bits/jungsiroo.py | 9 ----- two-sum/jungsiroo.py | 27 ------------- 3 files changed, 82 deletions(-) delete mode 100644 product-of-array-except-self/jungsiroo.py delete mode 100644 reverse-bits/jungsiroo.py delete mode 100644 two-sum/jungsiroo.py diff --git a/product-of-array-except-self/jungsiroo.py b/product-of-array-except-self/jungsiroo.py deleted file mode 100644 index d1fe06042..000000000 --- a/product-of-array-except-self/jungsiroo.py +++ /dev/null @@ -1,46 +0,0 @@ -class Solution: - def productExceptSelf(self, nums: List[int]) -> List[int]: - # Solution 1 :모든 수를 미리 곱해두기 - # 0을 포함한다면 미리 0의 개수를 체크해두기 - - n = len(nums) - ret = [0 for _ in range(n)] - """ - all_product = 1 - non_zero_product = 1 - zero_cnt = 0 - - for x in nums: - all_product *= x - - if x == 0: - zero_cnt += 1 - else: - non_zero_product *= x - - if zero_cnt > 1: - return ret - - for i in range(n): - if nums[i] == 0: - ans = non_zero_product - else: - ans = all_product // nums[i] - ret[i] = ans - return ret - """ - - # Solution 2 : w/o division - # prefix sum과 같이 풀이 진행 - # Tc : O(n) / Sc : O(n) - no extra memory - - prefix_pr, postfix_pr = 1, 1 - for i in range(n): - ret[i] = prefix_pr - prefix_pr *= nums[i] - for i in range(n-1, -1, -1): - ret[i] *= postfix_pr - postfix_pr *= nums[i] - return ret - - diff --git a/reverse-bits/jungsiroo.py b/reverse-bits/jungsiroo.py deleted file mode 100644 index e010b64f0..000000000 --- a/reverse-bits/jungsiroo.py +++ /dev/null @@ -1,9 +0,0 @@ -class Solution: - def reverseBits(self, n: int) -> int: - # TC : O(32) - # SC : O(32) - - bit_str = ''.join(reversed(format(n, 'b').zfill(32))) - return int(bit_str, 2) - - diff --git a/two-sum/jungsiroo.py b/two-sum/jungsiroo.py deleted file mode 100644 index 8a5f7a80f..000000000 --- a/two-sum/jungsiroo.py +++ /dev/null @@ -1,27 +0,0 @@ -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - """ - O(n^2) 보다 빠르게 풀기 위한 방법 : 정렬 후 투포인터 이용 - Tc : O(nlogn) - Sc : O(n) // queue 생성 - """ - n = len(nums) - queue = [] - - for i in range(n): - queue.append([nums[i], i]) - - queue.sort() - - start, end = 0, n-1 - sum_ = queue[start][0] + queue[end][0] - - while sum_ != target: - if sum_ > target: - end -= 1 - else: - start += 1 - sum_ = queue[start][0] + queue[end][0] - - return [queue[start][1], queue[end][1]] - From 6bccbb4d48397e60341d695c45b7bedff7f0e404 Mon Sep 17 00:00:00 2001 From: siroo Date: Tue, 17 Dec 2024 17:49:36 +0900 Subject: [PATCH 4/7] valid anagram solution --- valid-anagram/jungsiroo.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 valid-anagram/jungsiroo.py diff --git a/valid-anagram/jungsiroo.py b/valid-anagram/jungsiroo.py new file mode 100644 index 000000000..672bf0b8e --- /dev/null +++ b/valid-anagram/jungsiroo.py @@ -0,0 +1,30 @@ +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + + # sort and compare + # Tc : O(nlogn) / Sc : O(n)(Because of python timsort) + """ + s = sorted(s) + t = sorted(t) + for s_char, t_char in zip(s, t): + if s_char != t_char: + return False + return True + """ + + # dictionary to count letters + # Tc : O(n) / Sc : O(n) + letters_cnt = dict() + INF = int(1e6) + for s_char in s: + letters_cnt[s_char] = letters_cnt.get(s_char,0)+1 + + for t_char in t: + letters_cnt[t_char] = letters_cnt.get(t_char,INF)-1 + if letters_cnt[t_char] < 0: + return False + + return sum(letters_cnt.values()) == 0 + From 858ab7fbc00d86e97882a35bf96ac4f02366da71 Mon Sep 17 00:00:00 2001 From: siroo Date: Tue, 17 Dec 2024 18:04:14 +0900 Subject: [PATCH 5/7] climbing stairs solution --- climbing-stairs/jungsiroo.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 climbing-stairs/jungsiroo.py diff --git a/climbing-stairs/jungsiroo.py b/climbing-stairs/jungsiroo.py new file mode 100644 index 000000000..0ebcdba95 --- /dev/null +++ b/climbing-stairs/jungsiroo.py @@ -0,0 +1,18 @@ +class Solution: + def climbStairs(self, n: int) -> int: + """ + dynamic programming + dp[0] = 1 + dp[1] = 1 + dp[2] = dp[0] + dp[1] = 2 + dp[3] = dp[1] + dp[2] = 3 + ... + + Tc = O(n) / Sc = O(n) + """ + + dp = [1 for _ in range(n+1)] + for i in range(2, n+1): + dp[i] = dp[i-2] + dp[i-1] + return dp[n] + From 84fe4686650be116aaabc6471064b017fdd302d0 Mon Sep 17 00:00:00 2001 From: siroo Date: Fri, 20 Dec 2024 18:49:26 +0900 Subject: [PATCH 6/7] 3Sum solution --- 3sum/jungsiroo.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 3sum/jungsiroo.py diff --git a/3sum/jungsiroo.py b/3sum/jungsiroo.py new file mode 100644 index 000000000..ad8419cbb --- /dev/null +++ b/3sum/jungsiroo.py @@ -0,0 +1,41 @@ +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + # Naive Solution + # Tc : O(n^3) / Sc : O(nC_3) + + n = len(nums) + """ + for i in range(n-2): + for j in range(i+1,n-1): + for k in range(j+1, n): + if nums[i]+nums[j]+nums[k] == 0: + ret.add(tuple(sorted([nums[i], nums[j], nums[k]]))) + + ret = [x for x in ret] + return ret + """ + + # Better Solution + # two-sum question with fixed num (traversal in for loop) + # Tc : O(n^2) / Sc : O(n) + ret = [] + nums.sort() + + for i in range(n): + if i>0 and nums[i] == nums[i-1]: + continue + j, k = i+1, n-1 + + while j < k: + sum_ = nums[i] + nums[j] + nums[k] + if sum_ < 0 : j += 1 + elif sum_ > 0 : k -= 1 + else: + ret.append([nums[i], nums[j], nums[k]]) + j += 1 + + while nums[j] == nums[j-1] and j Date: Fri, 20 Dec 2024 20:02:32 +0900 Subject: [PATCH 7/7] Decode Ways solution --- decode-ways/jungsiroo.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 decode-ways/jungsiroo.py diff --git a/decode-ways/jungsiroo.py b/decode-ways/jungsiroo.py new file mode 100644 index 000000000..0488e6f1a --- /dev/null +++ b/decode-ways/jungsiroo.py @@ -0,0 +1,26 @@ +class Solution: + def numDecodings(self, s: str) -> int: + """ + dp 이용 : 가능한 경우의 수를 구해놓고 그 뒤에 붙을 수 있는가? + ex) 1234 + (1,2) -> (1,2,3) + (12) -> (12,3) + (1) -> (1,23) + + Tc : O(n) / Sc : O(n) + """ + if s[0] == '0': return 0 + + n = len(s) + dp = [0]*(n+1) + dp[0] = dp[1] = 1 + + for i in range(2, n+1): + one = int(s[i-1]) + two = int(s[i-2:i]) + + if 1<=one<=9: dp[i] += dp[i-1] + if 10<=two<=26 : dp[i] += dp[i-2] + + return dp[n] +