diff --git a/combination-sum/heypaprika.py b/combination-sum/heypaprika.py new file mode 100644 index 000000000..a94c06ed6 --- /dev/null +++ b/combination-sum/heypaprika.py @@ -0,0 +1,20 @@ +# 어렵네요ㅜ 보고 풀었습니다 + +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + ans = [] + def func(cur_remain, arr, idx): + if cur_remain == 0: + ans.append(list(arr)) + return + elif cur_remain < 0: + return + + for i in range(idx, len(candidates)): + arr.append(candidates[i]) + func(cur_remain - candidates[i], arr, i) + arr.pop() + + func(target, [], 0) + return ans + diff --git a/maximum-subarray/heypaprika.py b/maximum-subarray/heypaprika.py new file mode 100644 index 000000000..7493708b2 --- /dev/null +++ b/maximum-subarray/heypaprika.py @@ -0,0 +1,14 @@ +""" +복잡도 : 예상 -> 예상한 이유 + +시간 복잡도 : O(n) -> len(nums) 만큼 반복 +공간 복잡도 : O(n) -> len(nums) 크기의 배열 a 생성 +""" +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + a = [0] * len(nums) + a[0] = nums[0] + for i in range(1, len(nums)): + a[i] = max(nums[i], nums[i]+a[i-1]) + return max(a) + diff --git a/product-of-array-except-self/heypaprika.py b/product-of-array-except-self/heypaprika.py new file mode 100644 index 000000000..ad5b88d83 --- /dev/null +++ b/product-of-array-except-self/heypaprika.py @@ -0,0 +1,36 @@ +""" +복잡도 : 예상 -> 예상한 이유 + +시간 복잡도 : O(n) -> for 문이 여러번 있지만, len(nums)만큼 여러번 반복하므로 O(n) +공간 복잡도 : O(n) -> len(nums)만큼의 배열 하나가 더 생기므로 +""" +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + zeros = 0 + products = 1 + ans_list = [0] * len(nums) + + for i in range(len(nums)): + if nums[i] == 0: + zeros += 1 + products *= nums[i] + + if zeros == 1: + products = 1 + alived_i = -1 + for i in range(len(nums)): + if nums[i] == 0: + alived_i = i + continue + products *= nums[i] + ans_list[alived_i] = products + return ans_list + elif zeros >= 2: + return ans_list + + ans_list = [products] * len(nums) + for i in range(len(nums)): + ans_list[i] //= nums[i] + + return ans_list + diff --git a/reverse-bits/heypaprika.py b/reverse-bits/heypaprika.py new file mode 100644 index 000000000..cfbf85100 --- /dev/null +++ b/reverse-bits/heypaprika.py @@ -0,0 +1,15 @@ +""" +복잡도 : 예상 -> 예상한 이유 + +시간 복잡도 : O(1) -> 어떤 수가 들어오더라도 상수만큼 연산 +공간 복잡도 : O(1) -> 어떤 수가 들어오더라도 상수만큼 할당 +""" +class Solution: + def reverseBits(self, n: int) -> int: + ans = 0 + for i in range(32): + if n % 2 == 1: + ans += 2**(31-i) + n = n // 2 + return ans + diff --git a/two-sum/heypaprika.py b/two-sum/heypaprika.py new file mode 100644 index 000000000..f2c193b6a --- /dev/null +++ b/two-sum/heypaprika.py @@ -0,0 +1,19 @@ +""" +복잡도 : 예상 -> 예상한 이유 + +시간 복잡도 : O(n) -> 딕셔너리 키로 검색하는 것은 O(1), 따라서 for 문 1개로 O(n) +공간 복잡도 : O(n) -> n 수 만큼 반복되면서 값이 할당됨. +""" +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + num_dict = {} + n = len(nums) + + for i in range(n): + num_A = nums[i] + num_B = target - num_A + if num_B in num_dict: + return [i, num_dict[num_B]] + num_dict[num_A] = i + return [] +