Skip to content
Closed
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]
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(이미 충분히 빠른 코드이지만) 첫 번째 for 루프에서 0의 갯수를 세어 2개 이상인 경우 바로 0 배열을 반환하면 더욱 빠른 코드가 될 것 같습니다!

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)
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"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오... float("-inf"), float("inf")는 각각 무슨 의미인지 궁금합니다!