Skip to content

Commit b0dbe33

Browse files
authored
Merge pull request #2409 from gcount85/main
[gcount85] WEEK 02 solutions
2 parents c96f055 + 307356c commit b0dbe33

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

โ€Ž3sum/gcount85.pyโ€Ž

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
# Intuition
3+
์ฒ˜์Œ์—๋Š” 1์ฃผ์ฐจ์˜ Two Sum ๋ฌธ์ œ ํ’€์ด๋ฅผ ์‘์šฉํ•˜์—ฌ, ๋ฐฐ์—ด ์ˆœํšŒ + ํ•ด์‹œ ํ…Œ์ด๋ธ” ๋งŒ๋“ค์–ด ๊ฐ’ ์ฐพ๊ธฐ๋ฅผ ์‹œ๋„ํ–ˆ์œผ๋‚˜
4+
์ •๋ ฌ + ํˆฌํฌ์ธํ„ฐ ํ’€์ด๊ฐ€ ๋” ๋น ๋ฅด๋ฏ€๋กœ ๊ทธ๋ ‡๊ฒŒ ์ œ์ถœํ–ˆ์Šต๋‹ˆ๋‹ค.
5+
6+
# Approach
7+
nums ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ -nums[i]๋ฅผ ํ•ฉ์œผ๋กœ ํ•˜๋Š” ๋‘ ์ˆ˜๋ฅผ ๋‚˜๋จธ์ง€ ๋ฐฐ์—ด ๋ถ€๋ถ„์—์„œ ์ฐพ๋Š”๋‹ค.
8+
์ฐพ์„ ๋•Œ๋Š” ์ •๋ ฌ & ํˆฌํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ค‘๋ณต์„ ๊ฑด๋„ˆ ๋›ฐ๋Š” ๋ฐฉ์‹์œผ๋กœ ์†๋„๋ฅผ ๋†’์ธ๋‹ค.
9+
10+
11+
# Complexity
12+
- Time complexity: ์ •๋ ฌ + ํˆฌํฌ์ธํ„ฐ ์ด์ค‘ ๋ฐ˜๋ณต์œผ๋กœ O(N^2)
13+
14+
- Space complexity: ์ •๋ ฌ ํ•˜๋Š”๋ฐ์— O(N) , answer ๋ฐฐ์—ด ์ƒ์„ฑํ•˜๋Š”๋ฐ์— O(M)
15+
"""
16+
17+
18+
class Solution:
19+
def threeSum(self, nums: list[int]) -> list[list[int]]:
20+
nums.sort() # O(NlogN)
21+
n = len(nums)
22+
answer = []
23+
24+
for i in range(n - 2): # ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ O(N^2)
25+
# ์ค‘๋ณต ์ œ๊ฑฐ
26+
if i > 0 and nums[i] == nums[i - 1]:
27+
continue
28+
29+
# nums[i]๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฉด ๋’ค๋„ ๋‹ค ์–‘์ˆ˜๋ผ ์ข…๋ฃŒ ๊ฐ€๋Šฅ
30+
if nums[i] > 0:
31+
break
32+
33+
left, right = i + 1, n - 1
34+
35+
while left < right:
36+
total = nums[i] + nums[left] + nums[right]
37+
38+
if total == 0:
39+
answer.append([nums[i], nums[left], nums[right]])
40+
left += 1
41+
right -= 1
42+
43+
# left/right ์ค‘๋ณต ์ œ๊ฑฐ
44+
while left < right and nums[left] == nums[left - 1]:
45+
left += 1
46+
while left < right and nums[right] == nums[right + 1]:
47+
right -= 1
48+
49+
elif total < 0:
50+
left += 1
51+
else:
52+
right -= 1
53+
54+
return answer
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
# Intuition
3+
๋งˆ์ง€๋ง‰ ๊ณ„๋‹จ์— ๋„์ฐฉํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š”, 2์Šคํ… ์ „์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜์™€ 1์Šคํ… ์ „์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํ•ฉํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค.
4+
5+
# Approach
6+
dp[n] = dp[n-1] + dp[n-2]
7+
8+
# Complexity
9+
- Time complexity: O(n)
10+
11+
- Space complexity: O(1)
12+
"""
13+
14+
15+
class Solution:
16+
def climbStairs(self, n: int) -> int:
17+
b = 1 # n์ด 1์ผ๋•Œ
18+
if n == 1:
19+
return b
20+
a = 2 # n์ด 2์ผ๋•Œ
21+
if n == 2:
22+
return a
23+
24+
for n in range(3, n + 1): # O(n)
25+
a, b = a + b, a
26+
return a
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
# Approach
3+
๋ชจ๋“  ์›์†Œ๋ผ๋ฆฌ ๊ณฑํ•œ ๋‹ค์Œ์— nums ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ๊ทธ ๊ฐ’์„ ํ•ด๋‹น ์›์†Œ๋กœ ๋‚˜๋ˆˆ๋‹ค.
4+
์ด๋•Œ 0์˜ ๊ฐœ์ˆ˜์™€ 0์„ ์ œ์™ธํ•œ ๊ณฑ์˜ ๊ฐ’๋„ ๊ฐ™์ด ๊ตฌํ•œ๋‹ค.
5+
0์ด 2๊ฐœ ์ด์ƒ์ด๋ฉด ์ •๋‹ต์€ ๋ชจ๋‘ 0์ด๋ฉฐ,
6+
0์ด 1๊ฐœ๋ฉด 0์„ ์ œ์™ธํ•œ ๊ณฑ์˜ ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค.
7+
8+
# Complexity
9+
- Time complexity: O(n)
10+
11+
- Space complexity: O(n)
12+
"""
13+
14+
15+
# Code
16+
class Solution:
17+
def productExceptSelf(self, nums: List[int]) -> List[int]:
18+
product = 1
19+
non_zero_product = 1
20+
zero_count = 0
21+
for num in nums: # O(n) Time complexity
22+
if num == 0:
23+
zero_count += 1
24+
non_zero_product *= 1
25+
else:
26+
non_zero_product *= num
27+
product *= num
28+
29+
if zero_count > 1:
30+
return [0] * len(nums) # O(n) space complexity
31+
32+
answer = [] # O(n) space complexity
33+
for i in range(len(nums)): # O(n) Time complexity
34+
if nums[i] == 0:
35+
answer.append(non_zero_product)
36+
else:
37+
answer.append(int(product / nums[i]))
38+
return answer

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
# Approach
3+
๋ฌธ์ž์—ด s๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๋”•์…”๋„ˆ๋ฆฌ word_dict๋ฅผ ๋งŒ๋“ค๊ณ ,
4+
๋ฌธ์ž์—ด t๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ word_dict์— ํ‚ค๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
5+
ํ‚ค๊ฐ€ ์—†์œผ๋ฉด: ์—๋„ˆ๊ทธ๋žจ ๋ถˆ๊ฐ€๋Šฅ.
6+
ํ‚ค๊ฐ€ ์žˆ์œผ๋ฉด: ๊ฐ’์—์„œ -1ํ•˜๊ณ , ์Œ์ˆ˜์ธ์ง€ ํ™•์ธํ•˜์—ฌ ์—๋„ˆ๊ทธ๋žจ ์—ฌ๋ถ€๋ฅผ ํŒ๋ณ„ํ•ฉ๋‹ˆ๋‹ค.
7+
8+
# Complexity
9+
- Time complexity: s์˜ ๊ธธ์ด๊ฐ€ N์ด๊ณ , t์˜ ๊ธธ์ด๊ฐ€ M์ผ ๋•Œ O(N+M)
10+
11+
- Space complexity: O(N+M)
12+
"""
13+
14+
from collections import defaultdict
15+
16+
17+
class Solution:
18+
def isAnagram(self, s: str, t: str) -> bool:
19+
if len(s) != len(t):
20+
return False
21+
22+
word_dict = defaultdict(int)
23+
for ch in s: # O(N)
24+
word_dict[ch] += 1
25+
26+
for ch in t: # O(M)
27+
if ch not in word_dict:
28+
return False
29+
word_dict[ch] -= 1
30+
if word_dict[ch] < 0:
31+
return False
32+
33+
return True
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
# https://leetcode.com/problems/validate-binary-search-tree/description/
3+
# Intuition
4+
BST๋ฅผ ์ค‘์œ„ ์ˆœํšŒํ•˜๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๊ฐ’์ด ์ •๋ ฌ๋œ๋‹ค๋Š” ์ ์— ์ฐฉ์•ˆํ–ˆ์Šต๋‹ˆ๋‹ค.
5+
6+
# Complexity
7+
- Time complexity: ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ N์ด๋ผ๊ณ  ํ•  ๋•Œ, O(N)
8+
9+
- Space complexity: ์žฌ๊ท€๋กœ ํŠธ๋ฆฌ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ํ˜ธ์ถœ ์Šคํƒ์ด ์Œ“์ด๋Š”๋ฐ
10+
ํŠธ๋ฆฌ์˜ ๋†’์ด๋ฅผ H๋ผ๊ณ  ํ•  ๋•Œ, ํ˜ธ์ถœ ์Šคํƒ์€ H ๋งŒํผ ์Œ“์ž…๋‹ˆ๋‹ค. => ๊ณต๊ฐ„ ๋ณต์žก๋„ O(H)
11+
"""
12+
13+
14+
class Solution:
15+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
16+
prev = [None]
17+
18+
def inorder(node):
19+
if not node:
20+
return True
21+
22+
if not inorder(node.left):
23+
return False
24+
25+
if prev[0] is not None and prev[0] >= node.val:
26+
return False
27+
prev[0] = node.val
28+
29+
return inorder(node.right)
30+
31+
return inorder(root)

0 commit comments

Comments
ย (0)