Skip to content
36 changes: 36 additions & 0 deletions 3sum/devyejin.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() # O(n)

n = len(nums)
result = []

for i in range(n - 2):
# target을 잡을때도 이전에 구했다면 패스
if i > 0 and nums[i] == nums[i - 1]:
continue

target = - nums[i]
left, right = i + 1, n - 1

while left < right:
two_sum = nums[left] + nums[right]

if two_sum == target:
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 two_sum < target:
left += 1
else:
right -= 1

return result
17 changes: 17 additions & 0 deletions climbing-stairs/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 2:
return n

dp = [0] * (n + 1)
dp[1], dp[2] = 1, 2

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

return dp[n]

20 changes: 20 additions & 0 deletions combination-sum/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def combinationSum(self, candidates: list[int], target: int) -> list[list[int]]:

def backtracking(start, total, result):
if total == target:
answer.append(result[:])
return

if sum(result) > target:
return

for i in range(start, len(candidates)):
result.append(candidates[i])
backtracking(i, total + candidates[i], result)
result.pop()

answer = []
backtracking(0, 0, [])
return answer

24 changes: 24 additions & 0 deletions decode-ways/devyejin..py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
def numDecodings(self, s: str) -> int:

if not s or s[0] == "0":
return 0

n = len(s)
dp = [0] * (n + 1)

dp[0] = 1
dp[1] = 1

for i in range(2, n + 1):
if s[i - 1] != "0":
dp[i] += dp[i - 1]

two_digit = int(s[i - 2: i])
if 10 <= two_digit <= 26:
dp[i] += dp[i - 2]

return dp[n]



10 changes: 10 additions & 0 deletions maximum-subarray/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def maxSubArray(self, nums: list[int]) -> int:
dp = [0] * len(nums)
dp[0] = nums[0]

for i in range(1, len(nums)):
dp[i] = max(nums[i], dp[i - 1] + nums[i])

return max(dp)

7 changes: 7 additions & 0 deletions number-of-1-bits/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def hammingWeight(self, n: int) -> int:
result = 0
while n:
result += n % 2
n //= 2
return result
16 changes: 16 additions & 0 deletions product-of-array-except-self/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def productExceptSelf(self, nums):
n = len(nums)
result = [1] * n

left_product = 1
for i in range(n):
result[i] = left_product
left_product *= nums[i]

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

return result
5 changes: 5 additions & 0 deletions valid-palindrome/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# isalnum() : 문자열이 영어, 한글 숫자 -> True , 아니라면 False
class Solution:
def isPalindrome(self, s: str) -> bool:
changed_s = [c.lower() for c in s if c.isalnum()]
return changed_s == changed_s[::-1]
51 changes: 51 additions & 0 deletions validate-binary-search-tree/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Definition for a binary tree node.
from collections import deque
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

def list_to_tree(arr):
if not arr:
return None
# -> 레벨로 주고 있음
root = TreeNode(arr[0])
queue = deque([root]) # 큐에 root를 넣고 시작 queue = deque() queue.append(root)와 동일
i = 1

while queue and i < len(arr):
current = queue.popleft()

if i < len(arr) and arr[i] is not None:
current.left = TreeNode(arr[i])
queue.append(current.left)
i += 1

if i < len(arr) and arr[i] is not None:
current.right = TreeNode(arr[i])
queue.append(current.right)
i += 1

return root


class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
self.prev = None
def inorder(node):
if not node:
return True

# 왼쪽 서브트리 확인
if not inorder(node.left):
return False

# 현재 노드 값 확인
if self.prev is not None and node.val <= self.prev:
return False
self.prev = node.val

return inorder(node.right)

return inorder(root)