From e2a74d05ef900ddde820974b2c7391a2896801ca Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Tue, 24 Dec 2024 18:19:55 +0900 Subject: [PATCH 1/7] reverse-bits solution --- reverse-bits/sungjinwi.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 reverse-bits/sungjinwi.py diff --git a/reverse-bits/sungjinwi.py b/reverse-bits/sungjinwi.py new file mode 100644 index 000000000..695f65fbb --- /dev/null +++ b/reverse-bits/sungjinwi.py @@ -0,0 +1,9 @@ +class Solution: + def reverseBits(self, n: int) -> int: + ret = 0 + for _ in range(31) : + ret |= n & 1 + ret <<= 1 + n >>= 1 + ret += n & 1 + return ret From 2064257591258155c21187a61bd4681dda2efeeb Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Tue, 24 Dec 2024 18:20:20 +0900 Subject: [PATCH 2/7] two-sum solution --- two-sum/sungjinwi.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 two-sum/sungjinwi.py diff --git a/two-sum/sungjinwi.py b/two-sum/sungjinwi.py new file mode 100644 index 000000000..3936bab65 --- /dev/null +++ b/two-sum/sungjinwi.py @@ -0,0 +1,6 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i in nums : + for j in nums : + if i != j and nums[i] + nums[j] == target : + return [i, j] From 1242b3eebf4e5d096f46caebea89c6a7c7d3aa77 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Fri, 27 Dec 2024 20:55:18 +0900 Subject: [PATCH 3/7] two-sum complexity --- two-sum/sungjinwi.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/two-sum/sungjinwi.py b/two-sum/sungjinwi.py index 3936bab65..dbf40b964 100644 --- a/two-sum/sungjinwi.py +++ b/two-sum/sungjinwi.py @@ -1,3 +1,10 @@ +""" + TC : for문 내부 for문 + O(N^2) + SC : 추가적인 메모리 쓰지 않으므로 + O(1) +""" + class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i in nums : From 549c2d8a23a3402799a67d67c69b5a3d3ffc12c1 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Fri, 27 Dec 2024 20:57:58 +0900 Subject: [PATCH 4/7] reverse-bit complexity --- reverse-bits/sungjinwi.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/reverse-bits/sungjinwi.py b/reverse-bits/sungjinwi.py index 695f65fbb..5bd2346d5 100644 --- a/reverse-bits/sungjinwi.py +++ b/reverse-bits/sungjinwi.py @@ -1,3 +1,10 @@ +""" + TC : n의 크기에 상관없이 32번 반복하므로 + O(1) + SC : 추가적인 메모리 쓰지 않으므로 + O(1) +""" + class Solution: def reverseBits(self, n: int) -> int: ret = 0 @@ -5,5 +12,5 @@ def reverseBits(self, n: int) -> int: ret |= n & 1 ret <<= 1 n >>= 1 - ret += n & 1 + ret |= n & 1 return ret From b55cdae7a50830e628bf484cb7ff9f22deb760bd Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Fri, 27 Dec 2024 21:00:56 +0900 Subject: [PATCH 5/7] product-of-array-except-self solution & complexity --- product-of-array-except-self/sungjinwi.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 product-of-array-except-self/sungjinwi.py diff --git a/product-of-array-except-self/sungjinwi.py b/product-of-array-except-self/sungjinwi.py new file mode 100644 index 000000000..89d4372b5 --- /dev/null +++ b/product-of-array-except-self/sungjinwi.py @@ -0,0 +1,18 @@ +""" + TC : for문 두번 반복하므로 O(2N) + -> O(N) + SC : answer 배열 외에 추가적인 메모리는 factor 변수 하나이므로 + -> O(1) +""" +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + answer = [1] * len(nums) + factor = 1 + for i in range(len(nums) - 1) : + factor *= nums[i] + answer[i + 1] *= factor + factor = 1 + for i in range(len(nums) - 1, 0, -1) : + factor *= nums[i] + answer[i - 1] *= factor + return answer From 242349754f9d3b56d221955778218a45969f0233 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Fri, 27 Dec 2024 22:07:43 +0900 Subject: [PATCH 6/7] maximum-product-subarray solution & complexity --- maximum-product-subarray/sungjinwi.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 maximum-product-subarray/sungjinwi.py diff --git a/maximum-product-subarray/sungjinwi.py b/maximum-product-subarray/sungjinwi.py new file mode 100644 index 000000000..e8f82028d --- /dev/null +++ b/maximum-product-subarray/sungjinwi.py @@ -0,0 +1,23 @@ +""" + /풀이 봐도 잘 이해 못해서 추가 코멘트/ + nums[i]가 그 전까지 subarray의 합 total보다 작은 음수인 케이스는 어떻게 되는거지 고민했는데 + ex) total : -1, nums[i] = -2 + 어차피 -1인 시점에 maxTotal이 업데이트 됐으므로 total은 nums[i]부터 더하기 시작한다는 의미로 -2로 설정한다는 것을 깨달음 + 따라서 이전까지 subarray의 합만 음수 양수 체크 + + TC : for문 한번 + => O(N) + SC : 추가적인 배열 등 메모리 쓰지 않으므로 + => O(1) +""" +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + total = nums[0] + maxTotal = nums[0] + for i in range(1, len(nums)) : + if (total < 0) : + total = nums[i] + else : + total += nums[i] + maxTotal = max(total, maxTotal) + return (maxTotal) From 626fc9f64fa21f134c524368deb3516121e6deb9 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Sat, 28 Dec 2024 00:44:08 +0900 Subject: [PATCH 7/7] combination-sum solution --- combination-sum/sungjinwi.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 combination-sum/sungjinwi.py diff --git a/combination-sum/sungjinwi.py b/combination-sum/sungjinwi.py new file mode 100644 index 000000000..0720a713c --- /dev/null +++ b/combination-sum/sungjinwi.py @@ -0,0 +1,20 @@ +""" + 시간 복잡도와 공간복잡도 추후 작성하겠습니다ㅠ + 풀이 보고 하루 뒤에 기억해서 해보려고 했는데도 한참 걸렸네요 +""" +class Solution: + def combinationSum(self, candidates: list[int], target: int) -> list[list[int]]: + ans = [] + comb = [] + def recur(n : int): + if sum(comb) > target : + return + elif sum(comb) == target : + return ans.append(comb.copy()) + else : + for i in range(n, len(candidates)) : + comb.append(candidates[i]) + recur(i) + comb.pop() + recur(0) + return ans