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
36 changes: 36 additions & 0 deletions 3sum/daiyongg-kim.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]]:
nums.sort()

result = []
n = len(nums)

for i in range(n - 2):

if i > 0 and nums[i] == nums[i - 1]:
continue

left = i + 1
right = n - 1
while left < right:
current_sum = nums[i] + nums[left] + nums[right]

if current_sum == 0:
result.append([nums[i], nums[left], nums[right]])

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 current_sum < 0:
left += 1

else:
right -= 1

return result
13 changes: 13 additions & 0 deletions climbing-stairs/daiyongg-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def climbStairs(self, n: int) -> int:
if n == 1:
return 1

prev, curr = 1, 2

for i in range(3, n+1):
next = prev + curr
prev = curr
curr = next

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

size = len(nums)

#initialize array as 1
result = [1] * size

left_pass = 1

for i in range(size):A
result[i] *= left_pass
left_pass *= nums[i]

right_pass = 1
for i in range(size-1, -1, -1):
result[i] *= right_pass
right_pass *= nums[i]

return result
44 changes: 44 additions & 0 deletions valid-anagram/daiyongg-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
first = {}
second = {}

for c in s:
if c in first:
first[c] += 1
else:
first[c] = 1

for c in t:
if c in second:
second[c] += 1
else:
second[c] = 1

return first == second
Copy link
Member

Choose a reason for hiding this comment

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

JavaScript에서 이 둘을 다르다고 리턴할텐데, Python의 딕셔너리는 동일하다고 리턴을 하는군요..!



# class Solution:
# def isAnagram(self, s: str, t: str) -> bool:
# first = {}

# if len(s) != len(t):
# return False

# for i in range(len(s)):
# if s[i] in first:
# first[s[i]] += 1
# else:
# first[s[i]] = 1

# for i in range(len(t)):
# if t[i] in first:
# first[t[i]] -= 1
# else:
# return False

# for i in first.values():
# if i != 0:
# return False

# return True
18 changes: 18 additions & 0 deletions validate-binary-search-tree/daiyongg-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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 checkBST(root, min_val, max_val):
if not root:
return True

if not (min_val < root.val < max_val):
return False
return checkBST(root.left, min_val, root.val) and checkBST(root.right, root.val, max_val)

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