Skip to content

Commit a06d1d9

Browse files
authored
Merge pull request #1798 from devyejin/main
[devyejin] WEEK 03 solutions
2 parents 0414281 + ae73269 commit a06d1d9

File tree

9 files changed

+186
-0
lines changed

9 files changed

+186
-0
lines changed

3sum/devyejin.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
nums.sort() # O(n)
4+
5+
n = len(nums)
6+
result = []
7+
8+
for i in range(n - 2):
9+
# target을 잡을때도 이전에 구했다면 패스
10+
if i > 0 and nums[i] == nums[i - 1]:
11+
continue
12+
13+
target = - nums[i]
14+
left, right = i + 1, n - 1
15+
16+
while left < right:
17+
two_sum = nums[left] + nums[right]
18+
19+
if two_sum == target:
20+
result.append([nums[i], nums[left], nums[right]])
21+
22+
while left < right and nums[left] == nums[left + 1]:
23+
left += 1
24+
25+
while left < right and nums[right] == nums[right - 1]:
26+
right -= 1
27+
28+
left += 1
29+
right -= 1
30+
31+
elif two_sum < target:
32+
left += 1
33+
else:
34+
right -= 1
35+
36+
return result

climbing-stairs/devyejin.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def climbStairs(self, n):
3+
"""
4+
:type n: int
5+
:rtype: int
6+
"""
7+
if n <= 2:
8+
return n
9+
10+
dp = [0] * (n + 1)
11+
dp[1], dp[2] = 1, 2
12+
13+
for i in range(3, n + 1):
14+
dp[i] = dp[i-1] + dp[i-2]
15+
16+
return dp[n]
17+

combination-sum/devyejin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def combinationSum(self, candidates: list[int], target: int) -> list[list[int]]:
3+
4+
def backtracking(start, total, result):
5+
if total == target:
6+
answer.append(result[:])
7+
return
8+
9+
if sum(result) > target:
10+
return
11+
12+
for i in range(start, len(candidates)):
13+
result.append(candidates[i])
14+
backtracking(i, total + candidates[i], result)
15+
result.pop()
16+
17+
answer = []
18+
backtracking(0, 0, [])
19+
return answer
20+

decode-ways/devyejin..py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
4+
if not s or s[0] == "0":
5+
return 0
6+
7+
n = len(s)
8+
dp = [0] * (n + 1)
9+
10+
dp[0] = 1
11+
dp[1] = 1
12+
13+
for i in range(2, n + 1):
14+
if s[i - 1] != "0":
15+
dp[i] += dp[i - 1]
16+
17+
two_digit = int(s[i - 2: i])
18+
if 10 <= two_digit <= 26:
19+
dp[i] += dp[i - 2]
20+
21+
return dp[n]
22+
23+
24+

maximum-subarray/devyejin.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def maxSubArray(self, nums: list[int]) -> int:
3+
dp = [0] * len(nums)
4+
dp[0] = nums[0]
5+
6+
for i in range(1, len(nums)):
7+
dp[i] = max(nums[i], dp[i - 1] + nums[i])
8+
9+
return max(dp)
10+

number-of-1-bits/devyejin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def hammingWeight(self, n: int) -> int:
3+
result = 0
4+
while n:
5+
result += n % 2
6+
n //= 2
7+
return result
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def productExceptSelf(self, nums):
3+
n = len(nums)
4+
result = [1] * n
5+
6+
left_product = 1
7+
for i in range(n):
8+
result[i] = left_product
9+
left_product *= nums[i]
10+
11+
right_product = 1
12+
for i in range(n - 1, -1, -1):
13+
result[i] *= right_product
14+
right_product *= nums[i]
15+
16+
return result

valid-palindrome/devyejin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# isalnum() : 문자열이 영어, 한글 숫자 -> True , 아니라면 False
2+
class Solution:
3+
def isPalindrome(self, s: str) -> bool:
4+
changed_s = [c.lower() for c in s if c.isalnum()]
5+
return changed_s == changed_s[::-1]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Definition for a binary tree node.
2+
from collections import deque
3+
class TreeNode:
4+
def __init__(self, val=0, left=None, right=None):
5+
self.val = val
6+
self.left = left
7+
self.right = right
8+
9+
def list_to_tree(arr):
10+
if not arr:
11+
return None
12+
# -> 레벨로 주고 있음
13+
root = TreeNode(arr[0])
14+
queue = deque([root]) # 큐에 root를 넣고 시작 queue = deque() queue.append(root)와 동일
15+
i = 1
16+
17+
while queue and i < len(arr):
18+
current = queue.popleft()
19+
20+
if i < len(arr) and arr[i] is not None:
21+
current.left = TreeNode(arr[i])
22+
queue.append(current.left)
23+
i += 1
24+
25+
if i < len(arr) and arr[i] is not None:
26+
current.right = TreeNode(arr[i])
27+
queue.append(current.right)
28+
i += 1
29+
30+
return root
31+
32+
33+
class Solution:
34+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
35+
self.prev = None
36+
def inorder(node):
37+
if not node:
38+
return True
39+
40+
# 왼쪽 서브트리 확인
41+
if not inorder(node.left):
42+
return False
43+
44+
# 현재 노드 값 확인
45+
if self.prev is not None and node.val <= self.prev:
46+
return False
47+
self.prev = node.val
48+
49+
return inorder(node.right)
50+
51+
return inorder(root)

0 commit comments

Comments
 (0)