Skip to content

Commit b5435fd

Browse files
authored
Merge pull request #2104 from Seoya0512/main
[Seoya0512] WEEK03 Solutions
2 parents af82c08 + de096ec commit b5435fd

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Approach
3+
- target๊ฐ’์—์„œ candidate ๊ฐ’์„ ๋นผ๊ณ  ๊ทธ ๋ˆ„์ ํ•ฉ์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค๋Š” ํ๋ฆ„์€ ํŒŒ์•…ํ–ˆ์ง€์ง€๋งŒ ๊ตฌํ˜„์ด ์–ด๋ ค์› ์Šต๋‹ˆ๋‹ค.
4+
-๊ทธ๋ž˜์„œ ์•Œ๊ณ ๋‹ฌ๋ ˆ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ์ตœ๋Œ€ํ•œ ์ดํ•ดํ•˜๊ณ  ํ˜ผ์ž ์ž‘์„ฑํ•ด๋ณด๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค....
5+
'''
6+
class Solution:
7+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
8+
dp = [[] for _ in range(target + 1)]
9+
dp[0] = [[]]
10+
11+
for candidate in candidates:
12+
for num in range(candidate, target +1):
13+
for combination in dp[num - candidate]:
14+
dp[num].append(combination + [candidate])
15+
return dp[target]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
Approach
3+
- ์‹ญ์ง„๋ฒ•์„ ์ด์ง„๋ฒ•์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ์‹๊ณผ ๋ˆ„์ ํ•ฉ์„ ์‚ฌ์šฉํ•จ
4+
5+
Time Complexity: O(log n)
6+
- while ๋ฌธ์—์„œ ์ˆซ์ž(num)์„ ๊ณ„์†ํ•ด์„œ 2๋กœ ๋‚˜๋ˆ„๋Š”๋ฐ ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„
7+
8+
Space Complexity: O(1)
9+
- ์ƒ์ˆ˜ bits์™€ nums๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„
10+
'''
11+
class Solution:
12+
def hammingWeight(self, n: int) -> int:
13+
bits = 0
14+
# ์ฃผ์–ด์ง„ ์ˆซ์ž๋ฅผ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ ๊ฐ’์ด 1์ธ ๊ฒฝ์šฐ bits์— ๋ˆ„์ ํ•จ
15+
num = n
16+
while num != 0 :
17+
if num % 2 == 1 :
18+
bits += 1
19+
num //= 2
20+
return bits
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
Approach
3+
- ํŒŒ์ด์ฌ์—์„œ ์ œ๊ณต๋˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ž์—ด ํ•„ํ„ฐ๋ง ์ฒ˜๋ฆฌํ›„ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ๊ฐ’์„ ๋น„๊ต
4+
5+
Time Complexity: O(N)
6+
- ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด๋ฅผ N์ด๋ผ๊ณ  ํ•  ๋•Œ, ๋ฌธ์ž์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ํ•„ํ„ฐ๋งํ•˜๋Š” ๋ฐ O(N) ์‹œ๊ฐ„์ด ๊ฑธ๋ฆผ
7+
- ์ดํ›„ ํ•„ํ„ฐ๋ง๋œ ๋ฌธ์ž์—ด์„ ๋’ค์ง‘์–ด right ๊ฐ’์„ ๋งŒ๋“œ๋Š” ๋ฐ๋„ O(N) ์‹œ๊ฐ„์ด ๊ฑธ๋ฆผ
8+
- left์™€ right ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๋ฐ๋„ O(N) ์‹œ๊ฐ„์ด ๊ฑธ๋ฆผ
9+
10+
Space Complexity: O(N)
11+
- ํ•„ํ„ฐ๋ง๋œ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๋Š” ๋ฐ O(N) ๊ณต๊ฐ„์ด ํ•„์š”ํ•จ
12+
- left์™€ right ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐ๋„ ๊ฐ๊ฐ O(N) ๊ณต๊ฐ„์ด ํ•„์š”ํ•จ
13+
'''
14+
class Solution:
15+
def isPalindrome(self, s: str) -> bool:
16+
# ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๊ฒฝ์šฐ๋งŒ ์†Œ๋ฌธ์ž ๋ณ€ํ™˜ ํ•„ํ„ฐ๋ง
17+
char_str = [char.lower() for char in s if char.isalnum()]
18+
# ์™ผ์ชฝ ๊ฐ’
19+
left = ''.join(char_str)
20+
# ์˜ค๋ฅธ์ชฝ ๊ฐ’
21+
right = ''.join(char_str[::-1])
22+
23+
return left == right

0 commit comments

Comments
ย (0)