Skip to content

Commit a6b64aa

Browse files
authored
Merge pull request #2695 from Chanz82/main
[Chanz] WEEK 02 Solutions
2 parents 6312f3f + 85e32b9 commit a6b64aa

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

โ€Žclimbing-stairs/Chanz82.pyโ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
3+
def __init__(self):
4+
self.memo = dict()
5+
6+
def climbStairs(self, n: int) -> int:
7+
8+
# ๊ณต๊ฐ„๋ณต์žก๋„ : n์˜ ๊ฐœ์ˆ˜ ๋งŒํผ memo๊ฐ€ ํ• ๋‹น๋˜๋ฏ€๋กœ o(n)
9+
# ์‹œ๊ฐ„๋ณต์žก๋„ : ๊ฐ ์นธ์— ๋Œ€ํ•ด์„œ ๊ณ„์‚ฐ์ด 1๋ฒˆ์”ฉ๋งŒ ๋˜๋ฏ€๋กœ o(n)
10+
11+
# base
12+
if n <= 2 :
13+
return n
14+
15+
# ์ด๋ฏธ ๊ณ„์‚ฐ ํ•œ ๊ฐ’์ด๋ผ๋ฉด ๋ฐ˜ํ™˜
16+
if n in self.memo:
17+
return self.memo[n]
18+
19+
# 1์นธ ์ „๊ณผ 2์นธ ์ „์˜ ๊ฒฐ๊ณผ๋ฅผ ํ•ฉํ•œ ๊ฒƒ์„ ๋ฐ˜ํ™˜.
20+
result = self.climbStairs(n-2) + self.climbStairs(n-1)
21+
self.memo[n] = result
22+
23+
return result
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
4+
# ๊ณต๊ฐ„ ๋ณต์žก๋„ : 2๊ฐœ์˜ list๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(2n)=O(n)
5+
# ์‹œ๊ฐ„ ๋ณต์žก๋„ : nums๋ฅผ ์„ธ๋ฒˆ ์ˆœํšŒ o(n)
6+
n = len(nums)
7+
left = [1] * n
8+
right = [1] * n
9+
10+
for idx in range(1, n):
11+
left[idx] = left[idx-1] * nums[idx-1]
12+
13+
for idx in range(n-2, -1, -1):
14+
right[idx] = right[idx+1] * nums[idx+1]
15+
16+
return [left[i] * right[i] for i in range(n)]

โ€Žvalid-anagram/Chanz82.pyโ€Ž

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
hashmap = dict()
4+
5+
# ๊ณต๊ฐ„๋ณต์žก๋„ : hashmap์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๊ณต๊ฐ„๋ณต์žก๋„๋Š” O(N)์ž…๋‹ˆ๋‹ค.
6+
# ์‹œ๊ฐ„๋ณต์žก๋„ : ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด๋งŒํผ ํ•œ๋ฒˆ ์ˆœํšŒํ•˜๊ณ , ๋‹ค์‹œ ๋ฌธ์ž์—ด t์˜ ๊ธธ์ด๋งŒํผ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(s+t) => O(N)์ž…๋‹ˆ๋‹ค.
7+
for ch in s:
8+
hashmap[ch] = hashmap.get(ch, 0) + 1
9+
10+
for ch in t:
11+
if ch in hashmap:
12+
hashmap[ch] -= 1
13+
if hashmap[ch] == 0:
14+
del hashmap[ch]
15+
else:
16+
return False
17+
18+
return not hashmap
19+

0 commit comments

Comments
ย (0)