Skip to content

Commit 4b99484

Browse files
[dolphinflow86] WEEK 02 Solutions (#2677)
* valid anagram solution 1 * valid anagram optimized solution * climbing stairs solution 1 * product of array except self solution * 3 sum solution * validate binary search tree solution * add empty line
1 parent dcb60f8 commit 4b99484

5 files changed

Lines changed: 108 additions & 0 deletions

File tree

3sum/dolphinflow86.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 1) Fix one element first, and then treat others as two-sum problem. Use set to get rid of duplicate combination.
2+
# TC: O(N^2) where N is the length of nums.
3+
# SC: O(N) where N is the length of nums.
4+
class Solution:
5+
def threeSum(self, nums: list[int]) -> list[list[int]]:
6+
answer: Set[tuple[int,int,int]] = set()
7+
n = len(nums)
8+
nums.sort()
9+
10+
for i in range(n-2):
11+
if i > 0 and nums[i] == nums[i-1]: continue
12+
13+
target = -nums[i]
14+
nums_map: Dict[int, int] = {}
15+
for j in range(i + 1, n):
16+
comp = target - nums[j]
17+
if comp in nums_map:
18+
answer.add((nums[i], comp, nums[j]))
19+
nums_map[nums[j]] = j
20+
21+
return [list(triplet) for triplet in answer]

climbing-stairs/dolphinflow86.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 1) Recursion with memoization.
2+
# TC: O(N) where N is the given natural number.
3+
# SC: O(N) where N is the given natural number.
4+
class Solution:
5+
def climb_rec(self, n: int, step: int, memo: Dict[int, int]):
6+
if step > n: return 0
7+
if step in memo: return memo[step]
8+
if step == n: return 1
9+
10+
first_way = self.climb_rec(n, step + 1, memo)
11+
second_way = self.climb_rec(n, step + 2, memo)
12+
memo[step] = first_way + second_way
13+
return memo[step]
14+
15+
16+
def climbStairs(self, n: int) -> int:
17+
if n == 1: return 1
18+
19+
memo = {}
20+
return self.climb_rec(n, 1, memo) + self.climb_rec(n, 2, memo)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 1) Without using division operator and need to meet linear time complexity,
2+
# calculate left accumulated product and calculate right accumulated product except iself and then
3+
# product of left and right to get the result.
4+
# TC: O(N) where N is the length of nums
5+
# SC: O(N) where N is the length of nums
6+
class Solution:
7+
def productExceptSelf(self, nums: List[int]) -> List[int]:
8+
n = len(nums)
9+
answer = [1] * n
10+
acc = 1
11+
for i in range(1, n):
12+
acc *= nums[i-1]
13+
answer[i] = acc
14+
15+
acc = 1
16+
for i in range(n-2, -1, -1):
17+
acc *= nums[i+1]
18+
answer[i] *= acc
19+
20+
return answer

valid-anagram/dolphinflow86.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 1) Sort input strings and check if those are the same.
2+
# TC: O(NlogN) where N is the size of string s and t due to sorting
3+
# SC: O(N) where N is the length of string s and t to store sorted list.
4+
class Solution:
5+
def isAnagram(self, s: str, t: str) -> bool:
6+
return sorted(s) == sorted(t)
7+
8+
# 2) Using dict to store count of the first string and decrease back to see if there's negative count.
9+
# TC: O(N + M) where N is the length of s and M is the length of t.
10+
# SC: O(N + M) where N is the length of s and M is the length of t.
11+
class Solution:
12+
def isAnagram(self, s: str, t: str) -> bool:
13+
if len(s) != len(t): return False
14+
15+
char_map: Dict[str, int] = {}
16+
for ch in s:
17+
char_map[ch] = char_map.get(ch, 0) + 1
18+
19+
for ch in t:
20+
if ch not in char_map or char_map[ch] == 0:
21+
return False
22+
23+
char_map[ch] -= 1
24+
25+
return True
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 1) Validate the BST using min and max values for each node.
2+
# TC: O(N) where N is the number of nodes in the BST
3+
# SC: O(H) where H is the height of the BST
4+
5+
# Definition for a binary tree node.
6+
# class TreeNode:
7+
# def __init__(self, val=0, left=None, right=None):
8+
# self.val = val
9+
# self.left = left
10+
# self.right = right
11+
class Solution:
12+
def solve(self, node: Optional[TreeNode], min: int, max: int) -> bool:
13+
if node == None: return True
14+
15+
if node.val <= min or node.val >= max: return False
16+
17+
left = self.solve(node.left, min, node.val)
18+
right = self.solve(node.right, node.val, max)
19+
return left and right
20+
21+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
22+
return self.solve(root, float('-inf'), float('inf'))

0 commit comments

Comments
 (0)