From 566f0f9353540760d63d7d293610182466ace868 Mon Sep 17 00:00:00 2001 From: jinah92 Date: Fri, 27 Dec 2024 11:22:33 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20week3=20easy=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reverse-bits/jinah92.py | 14 ++++++++++++++ two-sum/jinah92.py | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 reverse-bits/jinah92.py create mode 100644 two-sum/jinah92.py diff --git a/reverse-bits/jinah92.py b/reverse-bits/jinah92.py new file mode 100644 index 000000000..4d2d8cf5e --- /dev/null +++ b/reverse-bits/jinah92.py @@ -0,0 +1,14 @@ +# O(1) time, O(1) space +class Solution: + def reverseBits(self, n: int) -> int: + stack = [] + while len(stack) < 32: + stack.append(n % 2) + n //=2 + + result, scale = 0, 1 + while stack: + result += stack.pop() * scale + scale *= 2 + + return result diff --git a/two-sum/jinah92.py b/two-sum/jinah92.py new file mode 100644 index 000000000..e8b204155 --- /dev/null +++ b/two-sum/jinah92.py @@ -0,0 +1,12 @@ +# O(n) time, O(n) space + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + num_set = {} + + for idx, num in enumerate(nums): + other_num = target - num + if other_num in num_set: + return [idx, num_set[other_num]] + else: + num_set[num] = idx From cbce8b10480b33d270f1ef7ba4a980c4768ccf59 Mon Sep 17 00:00:00 2001 From: jinah92 Date: Fri, 27 Dec 2024 11:48:47 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20week3=20medium=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=ED=92=80=EC=9D=B4(product-of-array-except-self)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- product-of-array-except-self/jinah92.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 product-of-array-except-self/jinah92.py diff --git a/product-of-array-except-self/jinah92.py b/product-of-array-except-self/jinah92.py new file mode 100644 index 000000000..68bb85e2b --- /dev/null +++ b/product-of-array-except-self/jinah92.py @@ -0,0 +1,21 @@ +# O(n) time, O(n) space +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + non_zero_product = math.prod(filter(lambda x: x != 0, nums)) + raw_product = math.prod(nums) + zero_total = nums.count(0) + + result = [] + + for num in nums: + if zero_total > 1: + result.append(0) + elif zero_total == 1: + if num == 0: + result.append(non_zero_product) + else: + result.append(raw_product) + else: + result.append(raw_product // num) + + return result From c184b940499b6c893222a5e25254847b52b2fee4 Mon Sep 17 00:00:00 2001 From: jinah92 Date: Fri, 27 Dec 2024 11:53:24 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20week3=20medium=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=ED=92=80=EC=9D=B4(combination-sum)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- combination-sum/jinah92.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 combination-sum/jinah92.py diff --git a/combination-sum/jinah92.py b/combination-sum/jinah92.py new file mode 100644 index 000000000..ba0500ba0 --- /dev/null +++ b/combination-sum/jinah92.py @@ -0,0 +1,19 @@ +# O(T) time, O(C^T) space +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + results, nums = [], [] + + def dfs(start, total): + if total > target: + return + if total == target: + results.append(nums[:]) + for i in range(start, len(candidates)): + num = candidates[i] + nums.append(num) + dfs(i, total + num) + nums.pop() + + dfs(0, 0) + + return results From 93fe9c34f4a4307016652e24c9145c6a93700473 Mon Sep 17 00:00:00 2001 From: jinah92 Date: Fri, 27 Dec 2024 17:27:56 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20week3=20medium=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=ED=92=80=EC=9D=B4(maximum-subarray)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maximum-subarray/jinah92.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 maximum-subarray/jinah92.py diff --git a/maximum-subarray/jinah92.py b/maximum-subarray/jinah92.py new file mode 100644 index 000000000..dcf0d88b5 --- /dev/null +++ b/maximum-subarray/jinah92.py @@ -0,0 +1,14 @@ +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + num_set = {} + result = -math.inf + + for idx, num in enumerate(nums): + if idx == 0: + num_set[idx] = max(nums[0], result) + else: + num_set[idx] = max(num, num_set[idx-1] + num) + tmp_sum = num_set[idx] + result = max(result, tmp_sum) + + return result