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
39 changes: 39 additions & 0 deletions 3sum/hyeri0903.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from itertools import combinations

class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
"""
time complexity : O(n^2)
space complexity : O(1)
"""
answer = []
nums.sort()
n = len(nums)

for i in range(n):
#skipped if nums[i] == nums[i-1] to avoid duplicate triplets
if i > 0 and nums[i] == nums[i-1]:
continue

#search with two pointer
left, right = i+1, n-1

while left < right:
total = nums[left] + nums[i] + nums[right]
if total == 0:
answer.append([nums[left], nums[i], nums[right]])

#move the pointers past duplicates
while left < right and nums[left] == nums[left+1]:
left += 1
while left < right and nums[right] == nums[right-1]:
right -= 1

left += 1
right -= 1
elif total < 0:
left += 1
else:
right -= 1

return answer
17 changes: 17 additions & 0 deletions climbing-stairs/hyeri0903.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def climbStairs(self, n: int) -> int:
"""
To find the number of distinct ways clibe to the top

time complexity: O(n)
space complexity: O(n)
"""
dp = [0] * (n+1)

for i in range(n+1):
if i == 0 or i == 1 or i == 2:
dp[i] = i
else:
dp[i] = dp[i-1] + dp[i-2]

return dp[n]
21 changes: 21 additions & 0 deletions product-of-array-except-self/hyeri0903.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
"""
time complexity : O(n)
space complexity : O(n)
"""
n = len(nums)
prefix = [1] * n
suffix = [1] * n
answer = [1] * n

for i in range(1, n):
prefix[i] = prefix[i-1] * nums[i-1]

for i in range(n-2, -1, -1):
suffix[i] = suffix[i+1] * nums[i+1]

for i in range(n):
answer[i] = prefix[i] * suffix[i]

return answer
29 changes: 29 additions & 0 deletions valid-anagram/hyeri0903.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
"""
Determine whether two strings are anagrams.

Time Complexity: O(n)
Space Complexity: O(k)
- n: length of the string
- k: number of unique characters
"""
if len(s) != len(t):
return False

dic_s = {}
dic_t = {}

for i in s:
if i in dic_s:
dic_s[i] += 1
else:
dic_s[i] = 1

for j in t:
if j in dic_t:
dic_t[j] += 1
else:
dic_t[j] = 1

return dic_s == dic_t
35 changes: 35 additions & 0 deletions validate-binary-search-tree/hyeri0903.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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:
"""
checking validation using inorder traversal
always greater than previous node's key

time complexity: O(n)

"""

prev = [None]

def inorder(node):
if not node:
return True

#search left sub tree first
if not inorder(node.left):
return False

if prev[0] is not None and node.val <= prev[0]:
return False

#set current node value
prev[0] = node.val
#search right sub tree
return inorder(node.right)

return inorder(root)
Loading