Skip to content

Commit

Permalink
Merge pull request #786 from heypaprika/main
Browse files Browse the repository at this point in the history
[croucs] Week 03
  • Loading branch information
SamTheKorean authored Dec 29, 2024
2 parents 688b8ea + 3762dae commit 740d882
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
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]

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 []

0 comments on commit 740d882

Please sign in to comment.