Skip to content

Commit 24659b5

Browse files
committed
solution: valid-anagram, climbing-stairs, product-of-array-except-self
1 parent aeffb46 commit 24659b5

File tree

4 files changed

+281
-0
lines changed

4 files changed

+281
-0
lines changed

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""
2+
# Intuition
3+
๊ณ„๋‹จ ์ •์ƒ์— ์˜ค๋ฅผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜ - 1,2 - ์ˆœ์—ด? -> X. ์ž๋ฆฌ์ˆ˜๊ฐ€ ์ผ์ •ํ•˜์ง€ ์•Š์Œ
4+
5+
# Approach
6+
์ ‘๊ทผ 1) 1 or 2 ๋กœ๋งŒ ์ด๋™ ๊ฐ€๋Šฅ
7+
8+
ํ’€์ด ์ฐธ๊ณ 
9+
- ๊ณ„๋‹จ์„ ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 2์นธ ๋ฐ–์— ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, 3๋ฒˆ์งธ ์นธ์— ๋ฐœ์„ ๋”›๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ”๋กœ ์•„๋ž˜ ์นธ์ธ 2๋ฒˆ์งธ ์นธ์ด๋‚˜ ์ ์–ด๋„ 1๋ฒˆ์งธ ์นธ์— ๋ฐ˜๋“œ์‹œ ๋จผ์ € ์˜ฌ๋ผ์™€์žˆ์–ด์•ผ ํ•จ.
10+
- ์ฆ‰, n ์นธ์— ๋ฐœ์„ ๋”›๊ธฐ์œ„ํ•ด์„œ๋Š” ๊ทธ ์ „์— n - 1 ์นธ์ด๋‚˜ n - 2 ์นธ๊นŒ์ง€ ์˜ฌ๋ผ์™€์™”์–ด์•ผ ํ•œ๋‹ค.
11+
12+
์ ‘๊ทผ 2)
13+
4 -> 5
14+
1 1 1 1
15+
2 1 1
16+
1 2 1
17+
1 1 2
18+
2 2
19+
20+
5 -> 8
21+
1 1 1 1 1
22+
2 2 1
23+
1 2 2
24+
2 1 2
25+
1 1 1 2
26+
1 1 2 1
27+
1 2 1 1
28+
2 1 1 1
29+
30+
n=5๊นŒ์ง€๋งŒ ๋ดค์„ ๋•Œ ๋Š˜์–ด๋‚˜๋Š” ๊ทœ์น™์ด ํ”ผ๋ณด๋‚˜์น˜์ˆ˜์—ด๊ณผ ๊ฐ™์Œ. (hint)
31+
32+
# Complexity
33+
- Time complexity
34+
- DP 1 : O(N)
35+
- DP 2 : O(N)
36+
37+
- Recursive 1 : O(2^N)
38+
- Recursive 2 (caching): O(N)
39+
40+
- Space complexity
41+
- DP 1 : O(N)
42+
- DP 2 : O(1)
43+
44+
- Recursive 1 : O(N)
45+
- Recursive 2 (caching): O(N)
46+
47+
"""
48+
49+
50+
# DP 2 (๊ณต๊ฐ„ ์ตœ์ ํ™”)
51+
class Solution:
52+
def climbStairs(self, n: int) -> int:
53+
54+
if n < 3:
55+
return n
56+
57+
pre, cur = 1, 2
58+
59+
for _ in range(n - 2):
60+
pre, cur = cur, pre + cur # ์ˆœ์„œ !
61+
62+
return cur
63+
64+
65+
""" DP 1
66+
class Solution:
67+
def climbStairs(self, n: int) -> int:
68+
69+
dp = {1:1, 2:2}
70+
71+
for i in range(3, n+1):
72+
dp[i] = dp[i-1] + dp[i-2]
73+
74+
return dp[n]
75+
"""
76+
77+
""" Memoization (์žฌ๊ท€ + ์บ์‹ฑ)
78+
class Solution:
79+
def climbStairs(self, n: int) -> int:
80+
memo = {}
81+
82+
def _climb(n):
83+
84+
if n not in memo:
85+
if n < 3:
86+
memo[n] = n
87+
else:
88+
memo[n] = _climb(n - 1) + _climb(n - 2)
89+
90+
return memo[n]
91+
92+
return _climb(n)
93+
"""
94+
95+
96+
""" Recursive
97+
class Solution:
98+
def climbStairs(self, n: int) -> int:
99+
100+
if n < 3:
101+
return n
102+
103+
return self.climbStairs(n-1) + self.climbStairs(n-2)
104+
"""

โ€Žhouse-robber/wozlsla.pyโ€Ž

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
# Intuition
3+
<!-- Describe your first thoughts on how to solve this problem. -->
4+
5+
# Approach
6+
<!-- Describe your approach to solving the problem. -->
7+
8+
# Complexity
9+
- Time complexity:
10+
11+
- Space complexity:
12+
13+
"""
14+
15+
16+
# DP
17+
class Solution:
18+
def rob(self, nums: List[int]) -> int:
19+
20+
# initialize dp array
21+
dp = [0] * (len(nums) + 1)
22+
23+
if nums:
24+
dp[1] = nums[0]
25+
26+
for n in range(2, len(dp)):
27+
# current house : nums[n-1]
28+
rob_current = nums[n - 1] + dp[n - 2]
29+
skip_current = dp[n - 1]
30+
31+
dp[n] = max(rob_current, skip_current)
32+
33+
return dp[-1]
34+
35+
36+
""" Recursive + Memoization
37+
class Solution:
38+
def rob(self, nums: List[int]) -> int:
39+
40+
memo = {}
41+
42+
def dfs(start):
43+
if start in memo:
44+
return memo[start]
45+
46+
if start >= len(nums):
47+
memo[start] = 0
48+
else:
49+
memo[start] = max(nums[start] + dfs(start + 2), dfs(start + 1))
50+
51+
return memo[start]
52+
53+
return dfs(0)
54+
"""
55+
56+
""" Recursive
57+
class Solution:
58+
def rob(self, nums: List[int]) -> int:
59+
60+
def dfs(start):
61+
if start >= nums(len):
62+
return 0
63+
64+
return max(nums[start] + dfs[start+2], dfs[start+1])
65+
66+
return dfs(0)
67+
"""
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
# Intuition
3+
ํ•˜๋‚˜์”ฉ ์ˆœํšŒํ•˜๋ฉด์„œ ๋‚˜๋จธ์ง€ ๊ฐ’๋“ค์„ ๊ณฑํ•จ
4+
5+
# Approach
6+
์ ‘๊ทผ 1) ์›ํ˜• ํ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด?
7+
ํ˜„์žฌ ์ œ์™ธํ•˜๋ ค๋Š” ์š”์†Œ๋ฅผ popleft()๋กœ ์ œ๊ฑฐํ•˜๊ณ , ๋‚˜๋จธ์ง€ ์š”์†Œ๋“ค์˜ ๊ณฑ์„ ๊ณ„์‚ฐํ•œ ๋’ค, ๋‹ค์‹œ ๊ทธ ์š”์†Œ๋ฅผ append()ํ•˜์—ฌ ๋‹ค์Œ ๋ฐ˜๋ณต์„ ์œ„ํ•ด ๋ฑ์˜ ์ˆœ์„œ๋ฅผ ํšŒ์ „
8+
9+
์ ‘๊ทผ 2)
10+
1 b c d --> 1 x bcd
11+
a 1 c d --> a x cd
12+
a b 1 d --> ab x d
13+
a b c 1 --> abc x 1
14+
15+
16+
# Complexity
17+
- Time complexity : O(N)
18+
- Space complexity : O(N)
19+
"""
20+
21+
from typing import List
22+
23+
24+
class Solution:
25+
def productExceptSelf(self, nums: List[int]) -> List[int]:
26+
27+
# ์ค‘๋ณต ๊ณ„์‚ฐ์„ ํ”ผํ•˜๋Š” ๋ฐฉ๋ฒ•?
28+
res_1 = [1] * len(nums) # [1, 1, 1, 1]
29+
res_2 = [1] * len(nums) # [1, 1, 1, 1]
30+
31+
for n in range(1, len(nums)): # [1, 1(a), 2(ab), 6(abc)]
32+
res_1[n] = res_1[n - 1] * nums[n - 1]
33+
34+
nums.reverse() # [4, 3, 2, 1]
35+
for n in range(1, len(nums)): # [1, 4(d), 12(cd), 24(bcd)]
36+
res_2[n] = res_2[n - 1] * nums[n - 1]
37+
38+
res_2.reverse()
39+
return [res_1[i] * res_2[i] for i in range(len(nums))]
40+
41+
42+
sol = Solution()
43+
print(sol.productExceptSelf([1, 2, 3, 4]))
44+
45+
46+
"""
47+
# deque : O(N^2)
48+
from collections import deque
49+
import math
50+
51+
class Solution:
52+
def productExceptSelf(self, nums: List[int]) -> List[int]:
53+
54+
nums = deque(nums)
55+
result = []
56+
57+
for _ in range(len(nums)): # O(N)
58+
tmp = nums.popleft()
59+
result.append(math.prod(nums)) # O(N)
60+
nums.append(tmp)
61+
62+
return result
63+
"""

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
# Intuition
3+
์• ๋„ˆ๊ทธ๋žจ : ์ˆœ์„œ๋งŒ ๋ณ€๊ฒฝ. ์š”์†Œ/๊ฐœ์ˆ˜ ๋™์ผ -> ๋น„๊ตํ•˜์—ฌ ๊ฐ™์€์ง€ ํ™•์ธ
4+
5+
# Approach
6+
ํ•ด์‹œํ…Œ์ด๋ธ”์˜ ํ‚ค-๊ฐ’(์š”์†Œ-๊ฐœ์ˆ˜) ์Œ์œผ๋กœ ์ €์žฅ
7+
- ์ง์ ‘ ์ˆœํšŒ : python interpreter ์‹คํ–‰ ๋ฐ get ๋ฉ”์„œ๋“œ ๋ฐ˜๋ณต ํ˜ธ์ถœ/์—…๋ฐ์ดํŠธ
8+
- Counter : ๋ฌธ์ž์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ๋ฌธ์ž์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ณผ์ •์ด ์ตœ์ ํ™”(C)๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ํŒŒ์ด์ฌ์œผ๋กœ ์ง์ ‘ ๋ฐ˜๋ณต๋ฌธ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ๋น ๋ฅด๊ฒŒ ๋™์ž‘
9+
10+
# Complexity
11+
- Time complexity: O(N+M)
12+
- Space complexity: O(K). ์‚ฌ์‹ค์ƒ O(1) -> ์˜์–ด ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž์—ด์˜ ๊ฒฝ์šฐ K๋Š” 26์œผ๋กœ ์ƒ์ˆ˜์— ๊ฐ€๊นŒ์›€
13+
14+
"""
15+
16+
from collections import Counter
17+
18+
19+
class Solution:
20+
def isAnagram(self, s: str, t: str) -> bool:
21+
return Counter(s) == Counter(t)
22+
23+
24+
"""
25+
class Solution:
26+
def isAnagram(self, s: str, t: str) -> bool:
27+
28+
# table_s = table_t = {} # '๋™์ผ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ'ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋ณ€ ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ ์ˆ˜์ • ์‹œ ๋ชจ๋‘ ๋ฐ˜์˜ ๋จ.
29+
table_s = {}
30+
table_t = {}
31+
32+
for i in s:
33+
table_s[i] = table_s.get(i, 0) + 1
34+
for e in t:
35+
table_t[e] = table_t.get(e, 0) + 1
36+
37+
return table_s == table_t
38+
39+
s = "anagram"
40+
t = "nagaram"
41+
42+
# print(Solution.isAnagram(s, t)) -> ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ์ฒ˜๋Ÿผ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ์–ด์„œ self๋ฅผ ์ž๋™ ์ „๋‹ฌํ•˜์ง€ ๋ชปํ•จ
43+
44+
# ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šคํ™” ํ•„์š”
45+
sol = Solution()
46+
print(sol.isAnagram(s, t))
47+
"""

0 commit comments

Comments
ย (0)