Skip to content

[mandoolala] WEEK 3 solutions #1333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 20 commits into from
21 changes: 21 additions & 0 deletions 3sum/mandoolala.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List

class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
answer = set()
sorted_nums = sorted(nums)

for i in range(len(nums) - 2):
low, high = i + 1, len(nums) - 1
while low < high:
three_sum = sorted_nums[i] + sorted_nums[low] + sorted_nums[high]
if three_sum == 0:
answer.add((sorted_nums[i], sorted_nums[low], sorted_nums[high]))
low += 1
high -= 1
elif three_sum < 0:
low += 1
elif three_sum > 0:
high -= 1
return list(answer)

12 changes: 12 additions & 0 deletions climbing-stairs/mandoolala.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def climbStairs(self, n: int) -> int:
if n == 1:
return 1
if n == 2:
return 2
dp = [0]*n
dp[0] = 1
dp[1] = 2
for i in range(2, n):
dp[i] = dp[i-1] + dp[i-2]
return dp[n-1]
Empty file added combination-sum/mandoolala.py
Empty file.
Empty file added decode-ways/mandoolala.py
Empty file.
Empty file added maximum-subarray/mandoolala.py
Empty file.
Empty file added number-of-1-bits/mandoolala.py
Empty file.
14 changes: 14 additions & 0 deletions product-of-array-except-self/mandoolala.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import List

class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
answer = [1] * len(nums)
left_product = 1
for i in range(len(nums) - 1):
left_product *= nums[i]
answer[i + 1] *= left_product
right_product = 1
for i in range(len(nums) - 1, 0, -1):
right_product *= nums[i]
answer[i - 1] *= right_product
return answer
3 changes: 3 additions & 0 deletions valid-anagram/mandoolala.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
return sorted(s) == sorted(t)
Empty file added valid-palindrome/mandoolala.py
Empty file.
20 changes: 20 additions & 0 deletions validate-binary-search-tree/mandoolala.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import Optional

# 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:
def traverse(node, low, high):
if not node:
return True
if not (low < node.val < high):
return False
return traverse(node.left, low, node.val) and traverse(node.right, node.val, high)

return traverse(root, float("-inf"), float("inf"))