Skip to content

Commit

Permalink
FEAT : solved 3 problems
Browse files Browse the repository at this point in the history
  • Loading branch information
aa601 committed Jan 18, 2025
1 parent c8e3ac8 commit 37aea7d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
15 changes: 15 additions & 0 deletions container-with-most-water/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def maxArea(self, height: List[int]) -> int:
right = len(height) - 1
left = 0
max_size = 0
for line in range(len(height)):
if left >= right:
break
cur_size = (right - left) * min(height[left], height[right])
if height[left] < height[right]: # 왼쪽이 작으면 left 이동
left += 1
else:
right -= 1
max_size = max(max_size, cur_size)
return max_size
23 changes: 23 additions & 0 deletions longest-increasing-subsequence/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
dp = [1 for _ in range(len(nums))]
prv = [-1 for _ in range(len(nums))]
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] < nums[j]:
if dp[i] + 1 > dp[j]:
dp[j] = dp[i] + 1
"""
prv [-1, -1, -1, 2, 2, 3, 5, 5]
dp [ 1, 1, 1, 2, 2, 3, 4, 4]
nums[10, 9, 2, 5, 3, 7, 101, 18]
nums[2] = 2
nums[4] = 3
nums[5] = 7
#dp[j] = max(dp[i] + 1, dp[j])
# dp[i] + 1 <= dp[j]일 때 dp[j] 갱신X
# LIS길이가 더 적거나 같은 상황. => LIS배열이 업데이트X
"""
return max(dp)

19 changes: 19 additions & 0 deletions valid-parentheses/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def isValid(self, s: str) -> bool:
# stack 활용, 짝이 맞으면 pop하기
stack = []
dic = dict(zip(")}]", "({[")) # close bracket : open bracket 형태로 사전 생성
for char in s:
if char in "({[": # open bracket인 경우 stack에 추가
stack.append(char)
elif char in ")}]" : # close bracket이면서
if not stack or stack.pop() != dic[char]:
return False

# len()함수 시간복잡도 먹음 O(n)
# => not stack 사용하면 개선됨
if len(stack) == 0: # stack이 비어있다면 모든 짝이 맞아서 pop되었으므로 true 반환
return True
else:
return False

0 comments on commit 37aea7d

Please sign in to comment.