Skip to content

Commit 2534d70

Browse files
Merge pull request #2411 from riveroverflows/main
[riveroverflows] WEEK 02 Solutions
2 parents 4aa9763 + fd52c87 commit 2534d70

3 files changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import *
2+
3+
4+
class Solution:
5+
"""
6+
TC: O(n)
7+
- memo = [-1] * (n+1): O(n)
8+
- for num in range(3, n+1): O(n)
9+
์ตœ์ข…: O(n)
10+
11+
SC: O(n)
12+
- memo: O(n)
13+
14+
ํ’€์ด:
15+
n๋ฒˆ์งธ ๊ณ„๋‹จ๊นŒ์ง€ ์˜ฌ๋ผ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋Š” (n-1๋ฒˆ์งธ ๋ฐฉ๋ฒ•์˜ ์ˆ˜) + (n-2๋ฒˆ์งธ ๋ฐฉ๋ฒ•์˜ ์ˆ˜).
16+
ํ”ผ๋ณด๋‚˜์น˜์ˆ˜์—ด๊ณผ ๋™์ผํ•œ ์ ํ™”์‹. memoization์œผ๋กœ ์ค‘๋ณต ๊ณ„์‚ฐ ๋ฐฉ์ง€.
17+
"""
18+
def climbStairs(self, n: int) -> int:
19+
if n < 2:
20+
return 1
21+
memo = [-1] * (n + 1)
22+
memo[1] = 1
23+
memo[2] = 2
24+
25+
for num in range(3, n + 1):
26+
memo[num] = memo[num - 1] + memo[num - 2]
27+
28+
return memo[n]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from typing import *
2+
3+
4+
class Solution:
5+
"""
6+
TC: O(n)
7+
- left ๋ฐฐ์—ด ์ƒ์„ฑ: O(n)
8+
- right ๋ฐฐ์—ด ์ƒ์„ฑ: O(n)
9+
- answer ๋ฐฐ์—ด ์ƒ์„ฑ: O(n)
10+
์ตœ์ข…: O(n)
11+
12+
SC: O(n)
13+
- left, right, answer ๋ฐฐ์—ด ๊ฐ๊ฐ O(n)
14+
์ตœ์ข…: O(n)
15+
16+
ํ’€์ด:
17+
๋‚˜๋ˆ„๊ธฐ ์—†์ด ํ’€๊ธฐ ์œ„ํ•ด ๊ฐ ์ธ๋ฑ์Šค ๊ธฐ์ค€ ์™ผ์ชฝ ์›์†Œ๋“ค์˜ ๊ณฑ(left), ์˜ค๋ฅธ์ชฝ ์›์†Œ๋“ค์˜ ๊ณฑ(right) ๋ฐฐ์—ด์„ ๋ณ„๋„๋กœ ๋งŒ๋“ค๊ณ 
18+
answer[i] = left[i-1] * right[i+1] ๋กœ ๊ณ„์‚ฐ.
19+
i=0์ด๋ฉด ์™ผ์ชฝ ๊ณฑ ์—†์œผ๋ฏ€๋กœ right[i+1]๋งŒ, i=๋งˆ์ง€๋ง‰์ด๋ฉด left[i-1]๋งŒ ์‚ฌ์šฉ.
20+
"""
21+
def productExceptSelf(self, nums: List[int]) -> List[int]:
22+
nums_len = len(nums)
23+
last_index = nums_len - 1
24+
25+
left = [0] * nums_len
26+
for i, num in enumerate(nums):
27+
if i == 0:
28+
left[i] = num
29+
continue
30+
left[i] = left[i - 1] * num
31+
32+
right = [0] * nums_len
33+
for i in range(last_index, -1, -1):
34+
if i == last_index:
35+
right[i] = nums[i]
36+
continue
37+
right[i] = right[i + 1] * nums[i]
38+
39+
answer = [0] * nums_len
40+
for i in range(nums_len):
41+
if i == 0:
42+
answer[i] = right[i + 1]
43+
continue
44+
if i == last_index:
45+
answer[i] = left[i - 1]
46+
continue
47+
answer[i] = left[i - 1] * right[i + 1]
48+
49+
return answer
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import *
2+
3+
4+
class Solution:
5+
"""
6+
ํ’€์ด:
7+
s์™€ t ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋ฉด anagram ์•„๋‹ˆ๋ฏ€๋กœ False๋กœ early return
8+
set์œผ๋กœ s์˜ ์ค‘๋ณต ๋ฌธ์ž ์ œ๊ฑฐ
9+
set์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ๋ฌธ์ž๊ฐ€ s์™€ t์—์„œ ๋“ฑ์žฅํ•˜๋Š” ํšŸ์ˆ˜๊ฐ€ ๋™์ผํ•œ์ง€ ํ™•์ธ
10+
๋‹ค๋ฅด๋ฉด False, ๋๊นŒ์ง€ ํ†ต๊ณผํ•˜๋ฉด True
11+
12+
TC: O(n+m)
13+
- if len(s) != len(t): O(1)
14+
- ss = set(s): O(n)
15+
- for c in ss: ์ตœ๋Œ€ 26ํšŒ ๋ฐ˜๋ณต โ†’ O(1)
16+
- s.count(c): O(n)
17+
- t.count(c): O(m)
18+
- ์ตœ์ข…: O(n+m)
19+
20+
SC: O(n)
21+
- ss = set(s): ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n)
22+
- ๊ทธ ์™ธ ์ƒ์ˆ˜: O(1)
23+
- ์ตœ์ข…: O(n)
24+
"""
25+
def isAnagram(self, s: str, t: str) -> bool:
26+
if len(s) != len(t):
27+
return False
28+
29+
ss = set(s)
30+
for c in ss:
31+
if s.count(c) != t.count(c):
32+
return False
33+
34+
return True

0 commit comments

Comments
ย (0)