Skip to content

Commit b8027d1

Browse files
authored
Merge pull request #1228 from shinsj4653/main
[shinsj4653] Week 02 Solutions
2 parents a42ab79 + 86e4ca9 commit b8027d1

File tree

5 files changed

+466
-0
lines changed

5 files changed

+466
-0
lines changed

โ€Ž3sum/shinsj4653.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
"""
2+
Inputs: ์ •์ˆ˜ ๋ฐฐ์—ด nums
3+
4+
Outputs: triplet ๋ชจ์Œ ๋ฐฐ์—ด (์ค‘๋ณต ์š”์†Œ X)
5+
6+
Constraints: 3 < = nums.length <= 3 * 10^3
7+
-10^5 <= nums[i] <= 10^5
8+
9+
Time Complexity:
10+
11+
๊ฐ ์ˆ˜๋“ค์˜ ์œ„์น˜ ๋‹ค๋ฅด๋ฉด์„œ, ํ•ฉ์ด 0์ด ๋˜๋Š” ์กฐํ•ฉ๋“ค์˜ ๋ชจ์Œ ๊ฒฐ๊ณผ
12+
n^3 ์€ ์•ˆ๋จ
13+
14+
ํ•˜์ง€๋งŒ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์กฐํ•ฉ ๊ตฌํ•˜๊ธฐ๋ฅผ ํšจ์œจํ™” ์‹œํ‚ค๋ฉด ๋ ๋“ฏ?
15+
16+
x 0 1 2 3 4 5
17+
0 [_,-1,0,1,-2,-5]
18+
1 [_,_,1,2,-1,-4]
19+
2 [_,_,_,
20+
3
21+
4
22+
5
23+
24+
n^2
25+
26+
27+
28+
0 [1 2]
29+
0 [1 3]
30+
0 [1 4]
31+
0 [1 5]
32+
33+
0 1 [2 3]
34+
0 1 [2 4]
35+
0 1 [2 5]
36+
37+
0 1 2[3 4]
38+
0 1 2[3 5]
39+
40+
0 1 2 3 [4 5]
41+
42+
์šฐ์„  ๋Œ€๊ด„ํ˜ธ ์•ˆ ๋‘ ์ˆ˜ ํ•ฉ ์‚ฌ์ „ ๋งŒ๋“ค๊ณ ,
43+
keys() ์ˆœํšŒํ•˜๋ฉฐ,
44+
key = a, b -> a๋ณด๋‹ค ์ž‘์€ ์ˆ˜ for๋ฌธ ๋Œ๋ฉฐ ํ•ฉ ๊ตฌํ•˜๊ธฐ?
45+
-> ๊ทธ๋Ÿผ ์‹œ๊ฐ„๋ณต์žก๋„ : O(n^3 ๋ณด๋‹ค ์‚ด์ง ์ž‘์€??)
46+
47+
ํ•˜์ง€๋งŒ ์ด ํ’€์ด๋กœ๋Š” ์ค‘๋ณต ํ›„๋ณด๊ฐ€ ๋‚˜์˜ด..
48+
49+
50+
Space Complexity: O(n^2)
51+
52+
๋Œ€๊ด„ํ˜ธ ๋‚ด ๋‘ ์ˆ˜ ์กฐํ•ฉ ๋งŒํผ์˜ ํฌ๊ธฐ๊ฐ€ ์‚ฌ์ „ ํฌ๊ธฐ
53+
54+
# 1์ฐจ ์ œ์ถœ ์ฝ”๋“œ
55+
56+
from collections import defaultdict
57+
58+
class Solution:
59+
def threeSum(self, nums: List[int]) -> List[List[int]]:
60+
n = len(nums)
61+
v = defaultdict(int)
62+
ret = []
63+
memo = set()
64+
65+
for i in range(1, n):
66+
for j in range(i + 1, n):
67+
v[(i, j)] = nums[i] + nums[j]
68+
69+
print(v)
70+
for key in v.keys():
71+
a, b = key
72+
print('key: a, b', a, b)
73+
74+
for i in range(a):
75+
if nums[i] + v[key] == 0 and \
76+
not (nums[i] in memo and nums[a] in memo and nums[b] in memo):
77+
print('sum zero!')
78+
memo.add(nums[i])
79+
memo.add(nums[a])
80+
memo.add(nums[b])
81+
ret.append([nums[i], nums[a], nums[b]])
82+
83+
84+
85+
return ret
86+
87+
ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งŒ ์ •๋‹ต..
88+
nums =
89+
[-1,0,1,2,-1,-4,-2,-3,3,0,4] ์ธ ๊ฒฝ์šฐ๋Š” ์˜ค๋‹ต
90+
91+
[ํšŒ๊ณ ]
92+
์™„ํƒ, dp๋กœ ํ•ด๋ด๋„ ์•ˆ ํ’€๋ฆฌ๋ฉด ํˆฌ ํฌ์ธํ„ฐ ์ƒ๊ฐํ•ด๋ณด๊ธฐ!
93+
"""
94+
95+
class Solution:
96+
def threeSum(self, nums: List[int]) -> List[List[int]]:
97+
tuples = set()
98+
nums.sort()
99+
100+
for i in range(len(nums) - 2):
101+
left, right = i + 1, len(nums) - 1
102+
103+
while left < right:
104+
three_sum = nums[i] + nums[left] + nums[right]
105+
if three_sum < 0:
106+
left += 1
107+
elif three_sum > 0:
108+
right -= 1
109+
else :
110+
tuples.add((nums[i], nums[left], nums[right]))
111+
left += 1
112+
right -= 1
113+
114+
return list(tuples)
115+

โ€Žclimbing-stairs/shinsj4653.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""
2+
๊ณ„๋‹จ ์˜ค๋ฅด๊ธฐ
3+
๋งจ ๊ผญ๋Œ€๊ธฐ ๊ฐ€๋Š”๋ฐ n steps๋งŒํผ ๊ฑธ๋ฆผ
4+
5+
๋งค๋ฒˆ ์˜ฌ๋ผ๊ฐ€๋Š”๋ฐ 1 or 2 ๊ณ„๋‹จ ์˜ค๋ฅด๊ธฐ ๊ฐ€๋Šฅ!
6+
7+
Inputs: n
8+
9+
Outputs: how many distinct ways to get to the top?
10+
11+
Constraints: 1 <= n <= 45
12+
13+
Time Complexity: O(2^n)
14+
15+
๊ณ„๋‹จ ์˜ค๋ฅด๋Š” ๋ฐฉ๋ฒ• ์ค‘, ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ๋ชจ๋“  ๊ฐ€์ง€ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
16+
์šฐ์„  ์™„ํƒ์œผ๋กœ ํ•ด๋ณด๊ณ , ๊ทธ ๋‹ค์Œ ์ตœ์ ๋ถ€๋ถ„๊ตฌ์กฐ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์ฒดํฌ
17+
18+
n = 2
19+
20+
1 1 -> dp[1]
21+
2 0 -> dp
22+
23+
2 dp(n - 2) + dp(n - 1) + 1
24+
25+
n = 3
26+
2 1
27+
1 1 1 => dp[2] ๊ฐ’
28+
29+
1 2 => ์—ฌ๊ธฐ์„  dp[2] ๋กœ ๊ฐ€๋ฉด ์•ˆ๋จ!
30+
31+
1 2
32+
1 1 1 => dp[2] ๊ฐ’
33+
34+
2 1 => ์ด๊ฑด dp[1] ๊ฐ’
35+
36+
37+
n = 4
38+
1 3 => dp[3]
39+
2 2 => dp[2]
40+
41+
n = 5
42+
2 2 1
43+
2 1 2
44+
1 2 2
45+
46+
n = 6
47+
48+
5 1
49+
4 2
50+
51+
n = 7
52+
53+
6 1
54+
5 4
55+
4 3
56+
57+
ํŠน์ • ์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” 1๊ณผ 2์˜ ๋ฐฐ์—ด ๊ฐ€์ง“์ˆ˜๋“ค์ด ์ •ํ•ด์ ธ์žˆ์Œ!!
58+
59+
ํ•œ ํ˜ธ์ถœ๋งˆ๋‹ค ๋ป—์–ด์ง€๋Š” ๊ฐ€์ง“์ˆ˜, ์ฆ‰ ํ˜ธ์ถœ์ˆ˜๋ฅผ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ์‹œ๊ฐ„๋ณต์žก๋„ ๋ชจ๋ฅด๊ฒ ์Œ
60+
61+
์ ํ™”์‹์„ ์–ด๋–ป๊ฒŒ ์„ธ์šฐ์ง€?
62+
63+
3
64+
1 2
65+
66+
๊ธฐ์ €์กฐ๊ฑด๋˜ ํ—ท๊ฐˆ... n 3 // 2 + 1
67+
68+
ํ•˜์ง€๋งŒ, ๋„์‹ํ™”ํ•ด๋ณด๋‹ˆ
69+
๊ฒฐ๊ตญ dp(n) = dp(n - 1) + dp(n - 2)
70+
71+
1 2
72+
1 1 1 => dp[2] ๊ฐ’
73+
74+
2 1 => ์ด๊ฑด dp[1] ๊ฐ’
75+
76+
Space Complexity: O(n)
77+
dp ๋ฐฐ์—ด n๋งŒํผ์˜ ํฌ๊ธฐ ์ง€๋‹˜
78+
79+
"""
80+
81+
82+
class Solution:
83+
def climbStairs(self, n: int) -> int:
84+
85+
if n == 1:
86+
return 1
87+
88+
dp = [0] * (n + 1)
89+
dp[0] = 1
90+
dp[1] = 1
91+
92+
def climb(n):
93+
94+
if dp[n]:
95+
return dp[n]
96+
97+
else:
98+
dp[n] += climb(n - 1) + climb(n - 2)
99+
return dp[n]
100+
101+
return climb(n)
102+
103+
# sol = Solution()
104+
# sol.climbStairs(3)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
Inputs: ์ •์ˆ˜ํ˜• ๋ฐฐ์—ด nums
3+
4+
Outputs: ์ •์ˆ˜ํ˜• ๋ฐฐ์—ด answer
5+
6+
Constraints: 2 <= nums.length <= 10^5
7+
-30 <= nums[i] <= 30
8+
The input is generated such that answer[i] is guaranteed to fit in a 32-bit integer.
9+
10+
Time Complexity: ๋ฐ˜๋“œ์‹œ o(n)
11+
12+
answer์˜ ๊ฐ ์›์†Œ๋Š” ๋ณธ์ธ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์›์†Œ๋“ค ๊ณฑํ•œ ๊ฒฐ๊ณผ
13+
14+
๋‚˜๋ˆ—์…ˆ ์—ฐ์‚ฐ๋„ ๋ถˆ๊ฐ€๋Šฅ
15+
์‚ฌ์ „?
16+
1 2 3 4
17+
2 4 6 8
18+
6 12 18 24
19+
24 48 72 96
20+
21+
dict[0] :
22+
dict[1] :
23+
dict[2] :
24+
dict[3] :
25+
26+
์Šคํƒ?? push, pop ํ•˜๋Š”๋ฐ o(1) ๊ฑธ๋ฆผ
27+
28+
(1,0) (2,1) (3,2) (4,3)
29+
30+
์Šคํƒ์—์„œ ๋บ€ ๋‹ค์Œ, ๋‹ค์‹œ ๋„ฃ์œผ๋ฉด while st์— ๊ฐ‡ํžˆ์ง€ ์•Š๋‚˜?
31+
32+
# ํ’€์ด ๋ณธ ์ดํ›„
33+
34+
nums 1 2 3 4
35+
36+
1 1 1 1
37+
38+
1 1 2 6 : ๊ธฐ์ค€ idx ์ „๊นŒ์ง€์˜ ๊ณฑ
39+
40+
24 12 4 1 : ๊ธฐ์ค€ idx ํ›„๊นŒ์ง€์˜ ๊ณฑ
41+
42+
=> ๋” ๊ฐœ์„ ๋œ ํ’€์ด: ๋ˆ„์ ๊ณฑ์„ ๋ฎ์–ด์”Œ์šฐ๋Š” ๋ฐฉ๋ฒ•
43+
44+
6
45+
24 12 8 6
46+
47+
24
48+
49+
50+
Space Complexity: O(1)
51+
product ๋ฐฐ์—ด์— ๊ณฑ ๊ฒฐ๊ณผ๋ฅผ ๋ฎ์–ด์”Œ์›Œ๋„ ๋ฌด๋ฐฉ
52+
53+
"""
54+
55+
56+
class Solution:
57+
def productExceptSelf(self, nums: List[int]) -> List[int]:
58+
59+
products = [1 for _ in range(len(nums))]
60+
61+
before = 1
62+
for i in range(len(nums) - 1):
63+
before *= nums[i]
64+
products[i + 1] *= before
65+
66+
after = 1
67+
for i in range(len(nums) - 1, 0, -1):
68+
after *= nums[i]
69+
products[i - 1] *= after
70+
71+
return products

โ€Žvalid-anagram/shinsj4653.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
Inputs: two strings : s, t
3+
4+
Outputs: t ๊ฐ€ s์˜ anagram์ธ์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€
5+
6+
Constraints:
7+
8+
1 <= s.length, t.length <= 5 * 10^4
9+
s and t consist of lowercase English letters.
10+
11+
Time Complexity: O(n)
12+
13+
๊ฐ ๋ฌธ์ž๋“ค์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋งŒ ๊ฐ™์œผ๋ฉด ๋˜์ง€ ์•Š๋‚˜?
14+
15+
s์˜ Counter ์ƒ์„ฑ
16+
t์˜ Counter ์ƒ์„ฑ
17+
18+
t Counter์˜ keys() ๋Œ๋ฉด์„œ,
19+
ํ•ด๋‹น ๊ฐ’์ด s Counter ๋ฐฐ์—ด key์— ์žˆ๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ๊ทธ key์˜ value๊ฐ’์ด ์„œ๋กœ ๊ฐ™์€์ง€ ์ฒดํฌ
20+
21+
Space Complexity: O(n)
22+
23+
"""
24+
25+
# ์ฒซ ์ฝ”๋“œ
26+
27+
from collections import defaultdict
28+
29+
30+
class Solution:
31+
def isAnagram(self, s: str, t: str) -> bool:
32+
s_dict, t_dict = defaultdict(int), defaultdict(int)
33+
34+
for ch in s:
35+
s_dict[ch] += 1
36+
37+
for ch in t:
38+
t_dict[ch] += 1
39+
40+
for key in t_dict.keys():
41+
if key not in t_dict or t_dict[key] != s_dict[key]:
42+
return False
43+
44+
return True
45+
46+
# ๋ฐ˜๋ก€ ๋ฐœ์ƒ
47+
48+
# s = "ab", t = "a"
49+
# ์–ด๋А ํ•œ ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์„ธ๋ฉด ์•ˆ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค
50+
# ๋‘ count ์‚ฌ์ „์„ ๋ชจ๋‘ ๋Œ์•„์•ผํ• ๋“ฏ. t keys()๋ฅผ ๊ธฐ์ค€์œผ๋กœ๋งŒ ๋Œ๋ฉด true๊ฐ€ ๋‚˜์™€๋ฒ„๋ฆผ. ๋‹ต์€ false์ธ๋ฐ
51+
52+
53+
from collections import defaultdict
54+
55+
56+
class Solution:
57+
def isAnagram(self, s: str, t: str) -> bool:
58+
s_dict, t_dict = defaultdict(int), defaultdict(int)
59+
60+
for ch in s:
61+
s_dict[ch] += 1
62+
63+
for ch in t:
64+
t_dict[ch] += 1
65+
66+
for key in t_dict.keys():
67+
if key not in s_dict or t_dict[key] != s_dict[key]:
68+
return False
69+
70+
for key in s_dict.keys():
71+
if key not in t_dict or t_dict[key] != s_dict[key]:
72+
return False
73+
74+
return True
75+

0 commit comments

Comments
ย (0)