Skip to content

Commit 740d882

Browse files
authored
Merge pull request #786 from heypaprika/main
[croucs] Week 03
2 parents 688b8ea + 3762dae commit 740d882

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

β€Žcombination-sum/heypaprika.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# μ–΄λ ΅λ„€μš”γ…œ 보고 ν’€μ—ˆμŠ΅λ‹ˆλ‹€
2+
3+
class Solution:
4+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
5+
ans = []
6+
def func(cur_remain, arr, idx):
7+
if cur_remain == 0:
8+
ans.append(list(arr))
9+
return
10+
elif cur_remain < 0:
11+
return
12+
13+
for i in range(idx, len(candidates)):
14+
arr.append(candidates[i])
15+
func(cur_remain - candidates[i], arr, i)
16+
arr.pop()
17+
18+
func(target, [], 0)
19+
return ans
20+

β€Žmaximum-subarray/heypaprika.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
λ³΅μž‘λ„ : μ˜ˆμƒ -> μ˜ˆμƒν•œ 이유
3+
4+
μ‹œκ°„ λ³΅μž‘λ„ : O(n) -> len(nums) 만큼 반볡
5+
곡간 λ³΅μž‘λ„ : O(n) -> len(nums) 크기의 λ°°μ—΄ a 생성
6+
"""
7+
class Solution:
8+
def maxSubArray(self, nums: List[int]) -> int:
9+
a = [0] * len(nums)
10+
a[0] = nums[0]
11+
for i in range(1, len(nums)):
12+
a[i] = max(nums[i], nums[i]+a[i-1])
13+
return max(a)
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
λ³΅μž‘λ„ : μ˜ˆμƒ -> μ˜ˆμƒν•œ 이유
3+
4+
μ‹œκ°„ λ³΅μž‘λ„ : O(n) -> for 문이 μ—¬λŸ¬λ²ˆ μžˆμ§€λ§Œ, len(nums)만큼 μ—¬λŸ¬λ²ˆ λ°˜λ³΅ν•˜λ―€λ‘œ O(n)
5+
곡간 λ³΅μž‘λ„ : O(n) -> len(nums)만큼의 λ°°μ—΄ ν•˜λ‚˜κ°€ 더 μƒκΈ°λ―€λ‘œ
6+
"""
7+
class Solution:
8+
def productExceptSelf(self, nums: List[int]) -> List[int]:
9+
zeros = 0
10+
products = 1
11+
ans_list = [0] * len(nums)
12+
13+
for i in range(len(nums)):
14+
if nums[i] == 0:
15+
zeros += 1
16+
products *= nums[i]
17+
18+
if zeros == 1:
19+
products = 1
20+
alived_i = -1
21+
for i in range(len(nums)):
22+
if nums[i] == 0:
23+
alived_i = i
24+
continue
25+
products *= nums[i]
26+
ans_list[alived_i] = products
27+
return ans_list
28+
elif zeros >= 2:
29+
return ans_list
30+
31+
ans_list = [products] * len(nums)
32+
for i in range(len(nums)):
33+
ans_list[i] //= nums[i]
34+
35+
return ans_list
36+

β€Žreverse-bits/heypaprika.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
λ³΅μž‘λ„ : μ˜ˆμƒ -> μ˜ˆμƒν•œ 이유
3+
4+
μ‹œκ°„ λ³΅μž‘λ„ : O(1) -> μ–΄λ–€ μˆ˜κ°€ λ“€μ–΄μ˜€λ”λΌλ„ μƒμˆ˜λ§ŒνΌ μ—°μ‚°
5+
곡간 λ³΅μž‘λ„ : O(1) -> μ–΄λ–€ μˆ˜κ°€ λ“€μ–΄μ˜€λ”λΌλ„ μƒμˆ˜λ§ŒνΌ ν• λ‹Ή
6+
"""
7+
class Solution:
8+
def reverseBits(self, n: int) -> int:
9+
ans = 0
10+
for i in range(32):
11+
if n % 2 == 1:
12+
ans += 2**(31-i)
13+
n = n // 2
14+
return ans
15+

β€Žtwo-sum/heypaprika.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
λ³΅μž‘λ„ : μ˜ˆμƒ -> μ˜ˆμƒν•œ 이유
3+
4+
μ‹œκ°„ λ³΅μž‘λ„ : O(n) -> λ”•μ…”λ„ˆλ¦¬ ν‚€λ‘œ κ²€μƒ‰ν•˜λŠ” 것은 O(1), λ”°λΌμ„œ for λ¬Έ 1개둜 O(n)
5+
곡간 λ³΅μž‘λ„ : O(n) -> n 수 만큼 λ°˜λ³΅λ˜λ©΄μ„œ 값이 할당됨.
6+
"""
7+
class Solution:
8+
def twoSum(self, nums: List[int], target: int) -> List[int]:
9+
num_dict = {}
10+
n = len(nums)
11+
12+
for i in range(n):
13+
num_A = nums[i]
14+
num_B = target - num_A
15+
if num_B in num_dict:
16+
return [i, num_dict[num_B]]
17+
num_dict[num_A] = i
18+
return []
19+

0 commit comments

Comments
Β (0)