Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[croucs] Week 03 #786

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions combination-sum/heypaprika.py
Original file line number Diff line number Diff line change
@@ -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

14 changes: 14 additions & 0 deletions maximum-subarray/heypaprika.py
Original file line number Diff line number Diff line change
@@ -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)

36 changes: 36 additions & 0 deletions product-of-array-except-self/heypaprika.py
Original file line number Diff line number Diff line change
@@ -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]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제에서 나눗셈을 사용하지 않고 풀라는 조건이 있어서요, 나눗셈을 쓰지 않는 방법도 고민해보시면 좋을 것 같습니다 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하! 감사합니다!!!


return ans_list

15 changes: 15 additions & 0 deletions reverse-bits/heypaprika.py
Original file line number Diff line number Diff line change
@@ -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

19 changes: 19 additions & 0 deletions two-sum/heypaprika.py
Original file line number Diff line number Diff line change
@@ -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 []

Loading