Skip to content

[jiyseo] WEEK 02 Solutions #1252

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

Merged
merged 4 commits into from
Apr 12, 2025
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
43 changes: 43 additions & 0 deletions 3sum/jiyseo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution(object):
def threeSum(self, nums):
# 시간복잡도 = O(N^3)
# length = len(nums)
# result = []
# for i in range(length - 1) :
# for j in range(i + 1, length) :
# s = - (nums[i] + nums[j])
# if s in nums[j + 1 : length] :
# result.append(sorted((nums[i], nums[j], s)))

# return(list(set(map(tuple, result))))

# 시간 복잡도 = O(N^2)
nums.sort()
length = len(nums)
result = []

for i in range(length - 2) :
if i > 0 and nums[i - 1] == nums[i] : # 같은 숫자인 경우 패스
continue
target = nums[i]
left = i + 1
right = length - 1

while left < right :
sum = target + nums[left] + nums[right]
if sum > 0 :
right -= 1
elif sum < 0 :
left += 1
else :
result.append([target, nums[left], nums[right]])
# 중복 숫자 건너뛰기
while left < right and nums[right] == nums[right - 1]:
right -= 1
while left < right and nums[left] == nums[left + 1]:
left += 1
right -= 1
left += 1

return result

15 changes: 15 additions & 0 deletions climbing-stairs/jiyseo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution(object):
def climbStairs(self, n):
# 시간복잡도 = O(N)
if n < 2 :
return n

arr = [0] * (n + 1)
arr[1] = 1
arr[2] = 2

for i in range(3, n + 1) :
arr[i] = arr[i - 1] + arr[i - 2]

return arr[n]

18 changes: 18 additions & 0 deletions product-of-array-except-self/jiyseo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution(object):
def productExceptSelf(self, nums):
# 시간복잡도 = O(N)
n = 1
arr = []
cnt = nums.count(0)
for i in nums : # 0을 제외한 값들의 곱
if i != 0 :
n *= i
for i in nums :
if i == 0 and cnt == 1: # nums에 0이 한개인 경우 나머지의 곱
arr.append(n)
elif cnt >= 1 : # nums에 0이 여러개인 경우 무조건 0
arr.append(0)
else : # 그 외의 경우
arr.append(n // i)
return(arr)

12 changes: 12 additions & 0 deletions valid-anagram/jiyseo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution(object):
def isAnagram(self, s, t):
# 시간복잡도 = O(N * (N + M))
if len(s) != len(t) :
return False

for i in set(s) :
if s.count(i) != t.count(i) :
return False

return True

18 changes: 18 additions & 0 deletions validate-binary-search-tree/jiyseo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
# 시간복잡도 = O(V + E)
def isValidBST(self, root):
def dfs(node, low, high) :
if not node :
return True
if not (low < node.val < high) :
return False
return dfs(node.left, low, node.val) and dfs(node.right, node.val, high)

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