Skip to content

[yyyyyyyyyKim] WEEK 02 solutions #1256

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 10 commits into from
Apr 14, 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
36 changes: 36 additions & 0 deletions 3sum/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:

answer = []

# 정렬
nums.sort()

for i in range(len(nums)-2):
# i 중복제거
if i > 0 and nums[i] == nums[i - 1]:
continue

left = i+1
right = len(nums)-1

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

# left 중복제거
while left < right and nums[left] == nums[left+1]:
left += 1
# right 중복제거
while left < right and nums[right] == nums[right-1]:
right -= 1

left += 1
right -= 1

elif nums[i] + nums[left] + nums[right] < 0:
left += 1
else:
right -= 1

return answer
12 changes: 12 additions & 0 deletions climbing-stairs/yyyyyyyyyKim.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 <= 3:
return n

a, b = 1, 2

for i in range(3,n+1):
a, b = b, a+b

return b
16 changes: 16 additions & 0 deletions product-of-array-except-self/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:

answer = [1]*len(nums)

left = 1
for i in range(len(nums)):
answer[i] *= left
left *= nums[i]

right = 1
for i in range(len(nums)-1,-1,-1):
answer[i] *= right
right *= nums[i]

return answer
6 changes: 6 additions & 0 deletions valid-anagram/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:

if sorted(s) == sorted(t):
return True
return False
Comment on lines +4 to +6
Copy link
Contributor

Choose a reason for hiding this comment

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

return sorted(s) == sorted(t); 한줄로 가능하지 않을까요?

19 changes: 19 additions & 0 deletions validate-binary-search-tree/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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:

# 깊이우선탐색(dfs), 재귀
def dfs(node, min_val, max_val):
if not node:
return True
if not (min_val < node.val < max_val):
return False
return (dfs(node.left, min_val, node.val) and
dfs(node.right, node.val, max_val))

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