Skip to content

Commit f9cabb5

Browse files
committed
solved
1 parent 857d78f commit f9cabb5

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

β€Žcombination-sum/pmjuu.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
6+
result = []
7+
8+
def backtrack(start, target, current_combination):
9+
# μ’…λ£Œ 쑰건
10+
if target == 0:
11+
result.append(list(current_combination))
12+
return
13+
if target < 0:
14+
return
15+
16+
# λ°±νŠΈλž˜ν‚Ή
17+
for i in range(start, len(candidates)):
18+
current_combination.append(candidates[i])
19+
backtrack(i, target - candidates[i], current_combination) # 같은 μ›μ†Œλ₯Ό μ—¬λŸ¬ 번 μ“Έ 수 μžˆλ„λ‘ iλ₯Ό κ·ΈλŒ€λ‘œ λ‘‘λ‹ˆλ‹€.
20+
current_combination.pop() # λŒμ•„κ°€μ„œ λ‹€μ‹œ μ‹œλ„ν•  수 μžˆλ„λ‘ μ›μ†Œλ₯Ό μ œκ±°ν•©λ‹ˆλ‹€.
21+
22+
backtrack(0, target, [])
23+
24+
return result
25+
26+
27+
# μ‹œκ°„ λ³΅μž‘λ„: O(n^t)
28+
# - 후보 λ¦¬μŠ€νŠΈμ—μ„œ 각 숫자λ₯Ό 선택할 수 있기 λ•Œλ¬Έμ—, n개의 후보λ₯Ό μ‚¬μš©ν•΄ t번의 탐색을 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
29+
# - λ”°λΌμ„œ μ΅œμ•…μ˜ 경우 탐색 νšŸμˆ˜λŠ” O(n^t)둜 λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.
30+
#
31+
# 곡간 λ³΅μž‘λ„: O(t)
32+
# - μž¬κ·€ 호좜 μŠ€νƒμ˜ κΉŠμ΄λŠ” μ΅œλŒ€ target 값인 t에 λΉ„λ‘€ν•˜λ―€λ‘œ, 곡간 λ³΅μž‘λ„λŠ” O(t)μž…λ‹ˆλ‹€.
33+
# - λ˜ν•œ, ν˜„μž¬κΉŒμ§€ μ„ νƒλœ μˆ«μžλ“€μ˜ 쑰합을 μ €μž₯ν•˜λŠ” 곡간도 μ΅œλŒ€ tκ°œκΉŒμ§€ μ €μž₯ν•˜λ―€λ‘œ, 곡간 λ³΅μž‘λ„λŠ” O(t)μž…λ‹ˆλ‹€.

β€Žmaximum-subarray/pmjuu.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def maxSubArray(self, nums: List[int]) -> int:
6+
largest_sum = -float('inf')
7+
current_sum = -float('inf')
8+
9+
for num in nums:
10+
current_sum = max(current_sum + num, num)
11+
largest_sum = max(largest_sum, current_sum)
12+
13+
return largest_sum
14+
15+
16+
# μ‹œκ°„ λ³΅μž‘λ„: O(n)
17+
# - nums 배열을 ν•œ 번 μˆœνšŒν•˜λ©° 각 μš”μ†Œμ— λŒ€ν•΄ μ΅œλŒ€ λΆ€λΆ„ λ°°μ—΄ 합을 κ³„μ‚°ν•˜λ―€λ‘œ μ‹œκ°„ λ³΅μž‘λ„λŠ” O(n)μž…λ‹ˆλ‹€.
18+
#
19+
# 곡간 λ³΅μž‘λ„: O(1)
20+
# - μΆ”κ°€λ‘œ μ‚¬μš©ν•˜λŠ” λ³€μˆ˜λŠ” largest_sumκ³Ό current_sum 두 κ°œλΏμ΄λ―€λ‘œ 곡간 λ³΅μž‘λ„λŠ” O(1)μž…λ‹ˆλ‹€.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def productExceptSelf(self, nums: List[int]) -> List[int]:
6+
n = len(nums)
7+
result = [1] * n
8+
9+
prefix = 1
10+
for i in range(n):
11+
result[i] *= prefix
12+
prefix *= nums[i]
13+
14+
suffix = 1
15+
for i in range(-1, -n-1, -1):
16+
result[i] *= suffix
17+
suffix *= nums[i]
18+
19+
return result
20+
21+
22+
# μ‹œκ°„ λ³΅μž‘λ„: O(n)
23+
# - μž…λ ₯ λ°°μ—΄ numsλ₯Ό 두 번 μˆœνšŒν•©λ‹ˆλ‹€.
24+
#
25+
# 곡간 λ³΅μž‘λ„: O(1)
26+
# - μΆ”κ°€ κ³΅κ°„μœΌλ‘œ μ‚¬μš©ν•˜λŠ” λ³€μˆ˜λŠ” prefix와 suffix뿐이며,
27+
# 좜λ ₯ λ°°μ—΄(result)은 μΆ”κ°€ κ³΅κ°„μœΌλ‘œ κ³„μ‚°ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

β€Žreverse-bits/pmjuu.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution:
2+
def reverseBits(self, n: int) -> int:
3+
# μ΄μ§„μˆ˜λ‘œ λ³€ν™˜ν•œ ν›„ '0b' 제거
4+
binary = bin(n)[2:]
5+
# 32λΉ„νŠΈ 길이에 맞게 μ•žμͺ½μ— 0을 채움
6+
binary = binary.zfill(32)
7+
# μ΄μ§„μˆ˜λ₯Ό λ’€μ§‘μŒ
8+
reversed_binary = binary[::-1]
9+
# λ’€μ§‘νžŒ μ΄μ§„μˆ˜λ₯Ό μ •μˆ˜λ‘œ λ³€ν™˜ν•˜μ—¬ λ°˜ν™˜
10+
return int(reversed_binary, 2)
11+
12+
13+
# μ‹œκ°„ λ³΅μž‘λ„: O(32)
14+
# - bin(): O(32)
15+
# - zfill(32): O(32)
16+
# - λ¬Έμžμ—΄ λ’€μ§‘κΈ° [::-1]: O(32)
17+
# - int(λ¬Έμžμ—΄, 2): O(32)
18+
# 총합: O(32) (μƒμˆ˜ μ‹œκ°„μœΌλ‘œ κ°„μ£Ό κ°€λŠ₯)
19+
20+
# 곡간 λ³΅μž‘λ„: O(32)
21+
# - 이진 λ¬Έμžμ—΄(binary)와 λ’€μ§‘νžŒ λ¬Έμžμ—΄(reversed_binary)을 μ €μž₯ν•˜λ―€λ‘œ O(32).
22+
23+
24+
class Solution:
25+
def reverseBits(self, n: int) -> int:
26+
result = 0
27+
28+
for i in range(32):
29+
# resultλ₯Ό μ™Όμͺ½μœΌλ‘œ 1λΉ„νŠΈ μ΄λ™ν•˜κ³  n의 λ§ˆμ§€λ§‰ λΉ„νŠΈλ₯Ό μΆ”κ°€
30+
result = (result << 1) | n & 1
31+
# n을 였λ₯Έμͺ½μœΌλ‘œ 1λΉ„νŠΈ 이동
32+
n >>= 1
33+
34+
return result
35+
36+
37+
# μ‹œκ°„ λ³΅μž‘λ„: O(32)
38+
# - 반볡문이 32번 μ‹€ν–‰λ˜λ©° 각 μž‘μ—…(λΉ„νŠΈ 이동 및 OR μ—°μ‚°)은 O(1).
39+
# 총합: O(32) (μƒμˆ˜ μ‹œκ°„μœΌλ‘œ κ°„μ£Ό κ°€λŠ₯)
40+
41+
# 곡간 λ³΅μž‘λ„: O(1)
42+
# - μΆ”κ°€λ‘œ μ‚¬μš©ν•˜λŠ” λ³€μˆ˜ result와 n만 μ €μž₯ν•˜λ―€λ‘œ μƒμˆ˜ 곡간.

β€Žtwo-sum/pmjuu.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
subtract_map = {}
7+
8+
for i, num in enumerate(nums):
9+
if num in subtract_map:
10+
return [i, subtract_map[num]]
11+
else:
12+
subtract_map[target - num] = i
13+
14+
15+
# μ‹œκ°„ λ³΅μž‘λ„: O(n)
16+
# - nums 배열을 ν•œ 번 μˆœνšŒν•˜λ©° 각 μš”μ†Œλ₯Ό ν™•μΈν•˜λ―€λ‘œ μ‹œκ°„ λ³΅μž‘λ„λŠ” O(n)μž…λ‹ˆλ‹€.
17+
#
18+
# 곡간 λ³΅μž‘λ„: O(n)
19+
# - μΆ”κ°€λ‘œ μ‚¬μš©ν•˜λŠ” subtract_map λ”•μ…”λ„ˆλ¦¬μ—λŠ” μ΅œμ•…μ˜ 경우 nums λ°°μ—΄μ˜ λͺ¨λ“  μš”μ†Œκ°€ μ €μž₯λ˜λ―€λ‘œ
20+
# 곡간 λ³΅μž‘λ„λŠ” O(n)μž…λ‹ˆλ‹€.

0 commit comments

Comments
Β (0)