Skip to content

Commit c59a6ba

Browse files
authored
Merge pull request #2033 from daiyongg-kim/main
[daiyongg-kim] WEEK 02 solutions
2 parents af17640 + ba5559a commit c59a6ba

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

3sum/daiyongg-kim.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
nums.sort()
4+
5+
result = []
6+
n = len(nums)
7+
8+
for i in range(n - 2):
9+
10+
if i > 0 and nums[i] == nums[i - 1]:
11+
continue
12+
13+
left = i + 1
14+
right = n - 1
15+
while left < right:
16+
current_sum = nums[i] + nums[left] + nums[right]
17+
18+
if current_sum == 0:
19+
result.append([nums[i], nums[left], nums[right]])
20+
21+
while left < right and nums[left] == nums[left + 1]:
22+
left += 1
23+
24+
while left < right and nums[right] == nums[right - 1]:
25+
right -= 1
26+
27+
left += 1
28+
right -= 1
29+
30+
elif current_sum < 0:
31+
left += 1
32+
33+
else:
34+
right -= 1
35+
36+
return result

climbing-stairs/daiyongg-kim.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
if n == 1:
4+
return 1
5+
6+
prev, curr = 1, 2
7+
8+
for i in range(3, n+1):
9+
next = prev + curr
10+
prev = curr
11+
curr = next
12+
13+
return curr
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
4+
size = len(nums)
5+
6+
#initialize array as 1
7+
result = [1] * size
8+
9+
left_pass = 1
10+
11+
for i in range(size):A
12+
result[i] *= left_pass
13+
left_pass *= nums[i]
14+
15+
right_pass = 1
16+
for i in range(size-1, -1, -1):
17+
result[i] *= right_pass
18+
right_pass *= nums[i]
19+
20+
return result

valid-anagram/daiyongg-kim.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
first = {}
4+
second = {}
5+
6+
for c in s:
7+
if c in first:
8+
first[c] += 1
9+
else:
10+
first[c] = 1
11+
12+
for c in t:
13+
if c in second:
14+
second[c] += 1
15+
else:
16+
second[c] = 1
17+
18+
return first == second
19+
20+
21+
# class Solution:
22+
# def isAnagram(self, s: str, t: str) -> bool:
23+
# first = {}
24+
25+
# if len(s) != len(t):
26+
# return False
27+
28+
# for i in range(len(s)):
29+
# if s[i] in first:
30+
# first[s[i]] += 1
31+
# else:
32+
# first[s[i]] = 1
33+
34+
# for i in range(len(t)):
35+
# if t[i] in first:
36+
# first[t[i]] -= 1
37+
# else:
38+
# return False
39+
40+
# for i in first.values():
41+
# if i != 0:
42+
# return False
43+
44+
# return True
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
def checkBST(root, min_val, max_val):
11+
if not root:
12+
return True
13+
14+
if not (min_val < root.val < max_val):
15+
return False
16+
return checkBST(root.left, min_val, root.val) and checkBST(root.right, root.val, max_val)
17+
18+
return checkBST(root, float('-inf'), float('inf'))

0 commit comments

Comments
 (0)