Skip to content

Commit d90d309

Browse files
authored
Merge pull request #2401 from hyeri0903/main
[hyeri0903] WEEK 02 solutions
2 parents d7de405 + df54080 commit d90d309

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

3sum/hyeri0903.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from itertools import combinations
2+
3+
class Solution:
4+
def threeSum(self, nums: List[int]) -> List[List[int]]:
5+
"""
6+
time complexity : O(n^2)
7+
space complexity : O(1)
8+
"""
9+
answer = []
10+
nums.sort()
11+
n = len(nums)
12+
13+
for i in range(n):
14+
#skipped if nums[i] == nums[i-1] to avoid duplicate triplets
15+
if i > 0 and nums[i] == nums[i-1]:
16+
continue
17+
18+
#search with two pointer
19+
left, right = i+1, n-1
20+
21+
while left < right:
22+
total = nums[left] + nums[i] + nums[right]
23+
if total == 0:
24+
answer.append([nums[left], nums[i], nums[right]])
25+
26+
#move the pointers past duplicates
27+
while left < right and nums[left] == nums[left+1]:
28+
left += 1
29+
while left < right and nums[right] == nums[right-1]:
30+
right -= 1
31+
32+
left += 1
33+
right -= 1
34+
elif total < 0:
35+
left += 1
36+
else:
37+
right -= 1
38+
39+
return answer

climbing-stairs/hyeri0903.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
"""
4+
To find the number of distinct ways clibe to the top
5+
6+
time complexity: O(n)
7+
space complexity: O(n)
8+
"""
9+
dp = [0] * (n+1)
10+
11+
for i in range(n+1):
12+
if i == 0 or i == 1 or i == 2:
13+
dp[i] = i
14+
else:
15+
dp[i] = dp[i-1] + dp[i-2]
16+
17+
return dp[n]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
"""
4+
time complexity : O(n)
5+
space complexity : O(n)
6+
"""
7+
n = len(nums)
8+
prefix = [1] * n
9+
suffix = [1] * n
10+
answer = [1] * n
11+
12+
for i in range(1, n):
13+
prefix[i] = prefix[i-1] * nums[i-1]
14+
15+
for i in range(n-2, -1, -1):
16+
suffix[i] = suffix[i+1] * nums[i+1]
17+
18+
for i in range(n):
19+
answer[i] = prefix[i] * suffix[i]
20+
21+
return answer

valid-anagram/hyeri0903.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
"""
4+
Determine whether two strings are anagrams.
5+
6+
Time Complexity: O(n)
7+
Space Complexity: O(k)
8+
- n: length of the string
9+
- k: number of unique characters
10+
"""
11+
if len(s) != len(t):
12+
return False
13+
14+
dic_s = {}
15+
dic_t = {}
16+
17+
for i in s:
18+
if i in dic_s:
19+
dic_s[i] += 1
20+
else:
21+
dic_s[i] = 1
22+
23+
for j in t:
24+
if j in dic_t:
25+
dic_t[j] += 1
26+
else:
27+
dic_t[j] = 1
28+
29+
return dic_s == dic_t
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
9+
"""
10+
checking validation using inorder traversal
11+
always greater than previous node's key
12+
13+
time complexity: O(n)
14+
15+
"""
16+
17+
prev = [None]
18+
19+
def inorder(node):
20+
if not node:
21+
return True
22+
23+
#search left sub tree first
24+
if not inorder(node.left):
25+
return False
26+
27+
if prev[0] is not None and node.val <= prev[0]:
28+
return False
29+
30+
#set current node value
31+
prev[0] = node.val
32+
#search right sub tree
33+
return inorder(node.right)
34+
35+
return inorder(root)

0 commit comments

Comments
 (0)