Skip to content

Commit 783ff8a

Browse files
authored
Merge pull request #1209 from sejineer/main
[sejineer] Week 02 solutions
2 parents 54d801e + 53fe546 commit 783ff8a

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

3sum/sejineer.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
시간 복잡도: O(N^2)
3+
공간 복잡도: O(N)
4+
"""
5+
class Solution:
6+
def threeSum(self, nums: List[int]) -> List[List[int]]:
7+
nums.sort()
8+
9+
result = set()
10+
11+
for i in range(len(nums) - 2):
12+
start, end = i + 1, len(nums) - 1
13+
while start < end:
14+
temp = nums[i] + nums[start] + nums[end]
15+
if temp == 0:
16+
result.add((nums[i], nums[start], nums[end]))
17+
start += 1
18+
end -= 1
19+
elif temp < 0:
20+
start += 1
21+
else:
22+
end -= 1
23+
24+
return list(result)

climbing-stairs/sejineer.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
시간 복잡도: O(N)
3+
공간 복잡도: O(N)
4+
"""
5+
class Solution:
6+
def climbStairs(self, n: int) -> int:
7+
dp = [0] * 46
8+
dp[1], dp[2] = 1, 2
9+
10+
for i in range(3, n + 1):
11+
dp[i] = dp[i - 1] + dp[i - 2]
12+
13+
return dp[n]
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
시간 복잡도: O(N)
3+
공간 복잡도: O(N)
4+
"""
5+
class Solution:
6+
def productExceptSelf(self, nums: List[int]) -> List[int]:
7+
prefix = [1] * len(nums)
8+
for i in range(len(nums) - 1):
9+
prefix[i + 1] = prefix[i] * nums[i]
10+
11+
suffix = [1] * len(nums)
12+
for i in range(len(nums) - 1, 0, -1):
13+
suffix[i - 1] = suffix[i] * nums[i]
14+
15+
result = []
16+
for i, j in zip(prefix, suffix):
17+
result.append(i * j)
18+
19+
return result

valid-anagram/sejineer.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
시간 복잡도: O(N)
3+
공간 복잡도: O(N)
4+
5+
코드 가독성 개선 코드:
6+
from collections import Counter
7+
8+
class Solution:
9+
def isAnagram(self, s: str, t: str) -> bool:
10+
return Counter(s) == Counter(t)
11+
"""
12+
from collections import Counter
13+
14+
class Solution:
15+
def isAnagram(self, s: str, t: str) -> bool:
16+
if len(s) != len(t):
17+
return False
18+
19+
s_counter = Counter(s)
20+
t_counter = Counter(t)
21+
22+
for num, freq in t_counter.items():
23+
if s_counter[num] != freq:
24+
return False
25+
26+
return True
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
시간 복잡도: O(N)
3+
공간 복잡도: O(N)
4+
"""
5+
class Solution:
6+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
7+
num_list = []
8+
9+
def dfs(node: TreeNode):
10+
if not node:
11+
return
12+
13+
dfs(node.left)
14+
num_list.append(node.val)
15+
dfs(node.right)
16+
17+
dfs(root)
18+
19+
mem = num_list[0]
20+
for i in range(1, len(num_list)):
21+
if mem >= num_list[i]:
22+
return False
23+
mem = num_list[i]
24+
return True

0 commit comments

Comments
 (0)