Skip to content

Commit c96f055

Browse files
authored
Merge pull request #2428 from ohkingtaek/main
[ohkingtaek] Week 2 solutions
2 parents eff1dff + 557a5af commit c96f055

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
"""
4+
n๋ฒˆ์งธ ๊ณ„๋‹จ์— ๋„๋‹ฌํ•˜๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.
5+
ํ•œ ๋ฒˆ์— 1์นธ ๋˜๋Š” 2์นธ์”ฉ ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ DP๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ dp[i] = dp[i-1] + dp[i-2] ์ ํ™”์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.
6+
์‹œ๊ฐ„๋ณต์žก๋„ O(n), ๊ณ„๋‹จ์„ ํ•œ ๋ฒˆ ์ˆœํšŒํ•˜๋ฉฐ ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
7+
"""
8+
dp = [0] * (n + 2)
9+
dp[1] = 1
10+
dp[2] = 2
11+
for i in range(3, n+1):
12+
dp[i] = dp[i-1] + dp[i-2]
13+
return dp[n]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
"""
4+
์ฒ˜์Œ์— ๋ชจ๋‘ ๊ณฑํ•œ๊ฑธ ๋‚˜๋ˆˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์œผ๋‚˜, 0์„ ๋‚˜๋ˆ„๊ธฐ ํž˜๋“ค๊ณ  ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ๋‹ต์ด ์žˆ์„๊ฑฐ๋ผ ์ƒ๊ฐํ•จ.
5+
๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ž์‹ ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์š”์†Œ๋“ค์˜ ๊ณฑ์„ ๊ณ„์‚ฐ.
6+
์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ๊ณฑ์„ ๊ณ„์‚ฐํ•˜์—ฌ answer[i]์— ์ €์žฅํ•˜๊ณ , ์˜ค๋ฅธ์ชฝ์—์„œ๋ถ€ํ„ฐ ๊ณฑ์„ ๊ณ„์‚ฐํ•˜์—ฌ answer[i]์— ๊ณฑํ•จ.
7+
์‹œ๊ฐ„๋ณต์žก๋„ O(n), ๋ฐฐ์—ด์„ ๋‘ ๋ฒˆ ์ˆœํšŒํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์‚ฐ.
8+
"""
9+
n = len(nums)
10+
answer = [1] * n
11+
12+
left = 1
13+
for i in range(n):
14+
answer[i] = left
15+
left *= nums[i]
16+
right = 1
17+
for i in range(n-1, -1, -1):
18+
answer[i] *= right
19+
right *= nums[i]
20+
21+
return answer
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from collections import Counter
2+
3+
class Solution:
4+
def isAnagram(self, s: str, t: str) -> bool:
5+
"""
6+
๋‘ ๋ฌธ์ž์—ด์ด anagram์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
7+
Counter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž ๋นˆ๋„์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ๋น„๊ตํ•œ๋‹ค.
8+
์‹œ๊ฐ„๋ณต์žก๋„ O(n), ๋ฌธ์ž์—ด์„ ํ•œ ๋ฒˆ ์ˆœํšŒํ•˜์—ฌ ๋นˆ๋„์ˆ˜๋ฅผ ๊ณ„์‚ฐ
9+
"""
10+
return True if Counter(s) == Counter(t) else False
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
3+
"""
4+
DFS๋กœ ๊ฐ ๋…ธ๋“œ๊ฐ€ low < node.val < high ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
5+
low์™€ high๋Š” ๊ฐ๊ฐ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์—์„œ์˜ ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์žฌ๊ท€์ ์œผ๋กœ ํ™•์ธํ•œ๋‹ค.
6+
"""
7+
def dfs(node, low, high):
8+
if not node:
9+
return True
10+
if not (low < node.val < high):
11+
return False
12+
13+
return dfs(node.left, low, node.val) and dfs(node.right, node.val, high)
14+
15+
return dfs(root, float('-inf'), float('inf'))

0 commit comments

Comments
ย (0)