Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions 3sum/WHYjun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
answer = []
sortedNums = sorted(nums)

for i in range(len(sortedNums)):
# skip if same to avoid dup
if i > 0 and sortedNums[i] == sortedNums[i-1]:
continue

# use two pointers
j = i + 1
k = len(sortedNums) - 1

while j < k:
total = sortedNums[i] + sortedNums[j] + sortedNums[k]

if total == 0:
answer.append([sortedNums[i], sortedNums[j], sortedNums[k]])
j += 1
# skip if same to avoid dup
while sortedNums[j] == sortedNums[j-1] and j < k:
j += 1
elif total < 0:
j += 1
else:
k -= 1

return answer
20 changes: 20 additions & 0 deletions climbing-stairs/WHYjun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def climbStairs(self, n: int) -> int:
dp = {}
return self.climbStairsRecursively(n, dp)

def climbStairsRecursively(self, n: int, dp: Dict[int, int]):
if n == 1:
dp[1] = 1
return dp[1]
if n == 2:
dp[2] = 2
return dp[2]

if n - 2 not in dp:
dp[n - 2] = self.climbStairsRecursively(n - 2, dp)

if n - 1 not in dp:
dp[n - 1] = self.climbStairsRecursively(n - 1, dp)

return dp[n - 2] + dp[n - 1]
35 changes: 35 additions & 0 deletions product-of-array-except-self/WHYjun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
answer = [1] * len(nums)
for i in range(len(nums)):
if i == 0:
continue
answer[i] = answer[i-1] * nums[i-1]

for j in range(len(nums)-1, -1, -1):
if j == len(nums) - 1:
product = 1
answer[j] *= product
product *= nums[j]

return answer

def productExceptSelfTwoArrays(self, nums: List[int]) -> List[int]:
leftProduct = [1] * len(nums)
rightProduct = [1] * len(nums)

for i in range(len(nums)):
if i == 0:
continue
leftProduct[i] = leftProduct[i-1] * nums[i-1]

for j in range(len(nums)-1, -1, -1):
if j == len(nums) - 1:
continue
rightProduct[j] = rightProduct[j+1] * nums[j+1]

for i in range(len(nums)):
leftProduct[i] *= rightProduct[i]

return leftProduct

17 changes: 17 additions & 0 deletions valid-anagram/WHYjun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False

sCount = {}
tCount = {}

for i in range(len(s)):
sCount[s[i]] = sCount.get(s[i], 0) + 1
tCount[t[i]] = tCount.get(t[i], 0) + 1

for char in sCount.keys():
if sCount.get(char, -1) != tCount.get(char, -1):
return False

return True
17 changes: 17 additions & 0 deletions validate-binary-search-tree/WHYjun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
# https://stackoverflow.com/a/37300370
return self.isValid(root, float('-inf'), float('inf'))

def isValid(self, node: Optional[TreeNode], low: float, high: float):
if node is None:
return True
if node.val <= low or node.val >= high:
return False
return self.isValid(node.left, low, node.val) and self.isValid(node.right, node.val, high)