Skip to content

Commit 4c9b0d0

Browse files
authored
Merge pull request #2098 from radiantchoi/main
2 parents bc10bcd + 375f0eb commit 4c9b0d0

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution:
2+
def traverse(self, candidates: List[int], target: int, result: List[List[int]], current: List[int], starting: int):
3+
# 쑰건을 λ§Œμ‘±ν•˜λŠ” 경우 결과에 μΆ”κ°€ν•˜κ³  λ°˜ν™˜ν•¨μœΌλ‘œμ¨ 콜 μŠ€νƒμ„ 더 이상 μŒ“μ§€ μ•ŠμŒ
4+
if target == 0:
5+
result.append(current[:])
6+
return
7+
8+
# μ•„μ˜ˆ 쑰건을 λ§Œμ‘±ν•˜μ§€ μ•ŠλŠ” 경우 λ°˜ν™˜ν•¨μœΌλ‘œμ¨ 콜 μŠ€νƒμ„ 더 이상 μŒ“μ§€ μ•ŠμŒ
9+
if target < 0:
10+
return
11+
12+
# starting μΈλ±μŠ€κ°€ ν•„μš”ν•œ μ΄μœ λŠ”, κ²°κ³Ό μΌ€μ΄μŠ€μ˜ μ›μ†Œ μˆœμ„œκ°€ μ€‘μš”ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έ
13+
# 즉, [2, 3]κ³Ό [3, 2]κ°€ 같은 μΌ€μ΄μŠ€μ΄κΈ° λ•Œλ¬Έ
14+
# μΈλ±μŠ€μ— λ”°λ₯Έ κ°€μ§€μΉ˜κΈ° 적용, 잠재적 쀑볡 μΌ€μ΄μŠ€ 제거
15+
for i in range(starting, len(candidates)):
16+
candidate = candidates[i]
17+
18+
# μ•žμ„œ νˆ¬μž…ν–ˆλ˜ current 배열을 인-λ©”λͺ¨λ¦¬ μ‘°μž‘ν•˜μ—¬ μ›μ†Œ μ‚½μž…
19+
current.append(candidate)
20+
21+
# 콜 μŠ€νƒμ— μ˜ν•΄, μ—¬κΈ°μ„œ ν˜ΈμΆœν•œ traverseκ°€ λ¨Όμ € result에 κ²°κ³Όλ₯Ό μΆ”κ°€
22+
# λ§Œμ•½ 이 "μžμ‹ 호좜"이 쑰건을 λ§Œμ‘±ν•œλ‹€λ©΄, μœ„μ—μ„œ μ²˜λ¦¬ν•˜κ³  λ°˜ν™˜
23+
self.traverse(candidates, target - candidate, result, current, i)
24+
25+
# 인-λ©”λͺ¨λ¦¬ μ‘°μž‘ν•œ current 배열을 원상볡ꡬ
26+
current.pop()
27+
28+
# λͺ¨λ“  μžμ‹ ν˜ΈμΆœμ„ λ§ˆλ¬΄λ¦¬μ§€μ€ traverse ν•¨μˆ˜λŠ” None을 λ°˜ν™˜
29+
30+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
31+
candidates.sort()
32+
33+
result = []
34+
current = [] # DFS μˆ˜ν–‰ 쀑, ν˜„μž¬κΉŒμ§€μ˜ κ²°κ³Όλ₯Ό 담을 λ°°μ—΄
35+
36+
self.traverse(candidates, target, result, current, 0)
37+
38+
return result
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def maxSubArray(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
5+
# μ›λž˜λΌλ©΄ dp 배열을 λ§Œλ“€μ–΄, i번째 μ›μ†ŒκΉŒμ§€ νƒμƒ‰ν•œ κ²°κ³Ό 쀑 μ΅œλŒ“κ°’μ„ 각각의 μΈλ±μŠ€μ— μ €μž₯
6+
# ν•˜μ§€λ§Œ μ§€κΈˆμ€ μ΅œλŒ“κ°’λ§Œ 남기면 λ˜λ―€λ‘œ 일차적 μ΅œμ ν™”
7+
current = nums[0]
8+
result = nums[0]
9+
10+
for i in range(1, len(nums)):
11+
current = max(current + nums[i], nums[i])
12+
result = max(result, current)
13+
14+
return result
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def hammingWeight(self, n: int) -> int:
3+
# n을 2μ§„μˆ˜λ‘œ λ³€ν™˜ν•œ λ¬Έμžμ—΄μ—μ„œ 1의 갯수λ₯Ό λ°˜ν™˜
4+
return len(list(filter(lambda x: x == "1", bin(n)[2:])))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
# μ„ μ œμ μœΌλ‘œ μ†Œλ¬Έμžλ‘œ λ°”κΎΌ λ¬Έμžμ—΄μ—μ„œ μ•ŒνŒŒλ²³μ΄λ‚˜ μˆ«μžμΈμ§€λ₯Ό 걸러 λ‚Έλ‹€.
4+
s = list(filter(lambda x: x.isalnum(), s.lower()))
5+
6+
# 처음과 λμ—μ„œ 각각 ν•œ μΉΈμ”© μ’ν˜€μ§€λŠ” 인덱슀의 κ΅¬ν˜„μ„ μœ„ν•΄ λ°°μ—΄μ˜ μ ˆλ°˜κΉŒμ§€λ§Œ 순회
7+
# 같은 μœ„κ³„μ— μžˆλŠ” κΈ€μžκ°€ κ°™μ§€ μ•Šμ„ 경우 early return
8+
for i in range(len(s) // 2):
9+
if s[i] != s[len(s) - i - 1]:
10+
return False
11+
12+
return True

0 commit comments

Comments
Β (0)