Skip to content

Commit 115b2b1

Browse files
authored
Merge pull request DaleStudy#2111 from changhyumm/main
[changhyumm] WEEK 03 solutions
2 parents d444820 + f4570e8 commit 115b2b1

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
ans = []
4+
def combination(index, cur_comb, cur_sum):
5+
if cur_sum == target:
6+
ans.append(cur_comb[:])
7+
return
8+
if cur_sum > target or index >= len(candidates):
9+
return
10+
cur_comb.append(candidates[index])
11+
combination(index, cur_comb, cur_sum + candidates[index])
12+
# ํ•ฉ์ด target์ด๋ž‘ ๊ฐ™๋˜์ง€, ํฌ๋˜์ง€, ๊ธธ์ด๋ฅผ ๋„˜๋˜์ง€ํ•˜๋ฉด return์œผ๋กœ ํƒˆ์ถœ
13+
# ๊ทธ ์™ธ์— ๋‹ค๋ฅธ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ด์•ผํ•˜๋ฏ€๋กœ
14+
# ๋งˆ์ง€๋ง‰๊บผ๋Š” ๋‹ค์‹œ ๋นผ๊ณ  ์–ด์จŒ๋“  index ๋„˜๊ฒจ์„œ ๋‹ค์‹œ combination ํ™•์ธํ•ด๋ด์•ผํ•จ
15+
cur_comb.pop()
16+
combination(index + 1, cur_comb, cur_sum)
17+
return ans
18+
19+
return combination(0, [], 0)

โ€Ždecode-ways/changhyumm.pyโ€Ž

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
memo = {}
4+
5+
def decode(index):
6+
# ์ด๋ฏธ ๊ณ„์‚ฐํ–ˆ์œผ๋ฉด ๋ฐ”๋กœ ๋ฐ˜ํ™˜
7+
if index in memo:
8+
return memo[index]
9+
10+
# ๊ธฐ์ € ์‚ฌ๋ก€
11+
## ๋๊นŒ์ง€ ์™”์œผ๋ฉด ์„ฑ๊ณต
12+
if index == len(s):
13+
return 1
14+
## 0์œผ๋กœ ์‹œ์ž‘ํ•˜๋ฉด ๋ถˆ๊ฐ€๋Šฅ
15+
if s[index] == '0':
16+
return 0
17+
18+
# ์žฌ๊ท€ ๊ณ„์‚ฐ
19+
ways = decode(index + 1)
20+
21+
if index + 1 < len(s) and int(s[index:index+2]) <= 26:
22+
ways += decode(index + 2)
23+
24+
# ๋ฉ”๋ชจ์ด์ œ์ด์…˜
25+
memo[index] = ways
26+
return ways
27+
28+
# ์‹œ๊ฐ„๋ณต์žก๋„, ๊ณต๊ฐ„๋ณต์žก๋„ O(n)
29+
30+
return decode(0)
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+
max_total = nums[0]
4+
total = 0
5+
# subarray๋Š” array์—์„œ ์„œ๋กœ ์ธ์ ‘ํ•ด์•ผํ•จ
6+
# ์ธ์ ‘ํ•œ ๊ฐ’์˜ ํ•ฉ์ด ๋งˆ์ด๋„ˆ์Šค์ธ ๊ฒฝ์šฐ, ๊ทธ๋ƒฅ ํ˜„์žฌ ๊ฐ’๋งŒ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ํ•ฉ๋ณด๋‹ค ํผ
7+
# total ์ด 0๋ณด๋‹ค ์ž‘์€๊ฒฝ์šฐ ๊ทธ๋ƒฅ 0์œผ๋กœ ๋ณ€๊ฒฝ
8+
for num in nums:
9+
if total < 0:
10+
total = 0
11+
total += num
12+
max_total = max(total, max_total)
13+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n), ๊ณต๊ฐ„๋ณต์žก๋„ O(1)
14+
return max_total
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def hammingWeight(self, n: int) -> int:
3+
count = 0
4+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(log n) ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ ๋•Œ๋ฌธ
5+
while n > 0:
6+
if n % 2 == 1:
7+
count += 1
8+
n = n // 2
9+
else:
10+
n = n / 2
11+
return count
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n)
4+
# loop
5+
s_list = [x.lower() for x in s if x.isalnum()]
6+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n)
7+
# loop + list pop()
8+
for i in range(len(s_list)//2):
9+
if s_list[i] != s_list.pop():
10+
return False
11+
return True

0 commit comments

Comments
ย (0)