diff --git a/container-with-most-water/hyeri0903.py b/container-with-most-water/hyeri0903.py new file mode 100644 index 0000000000..9375a08f8d --- /dev/null +++ b/container-with-most-water/hyeri0903.py @@ -0,0 +1,32 @@ +class Solution: + def maxArea(self, height: List[int]) -> int: + ''' + 1.문제: 가장 많은 양의 물을 저장할 수 있는 max value return + 2.조건 + - n: 높이를 의미, 최소 = 5, 최대 = 10^5 + - 원소값 최소 = 0, 최대 = 10^4 + 3.풀이 + - 높이는 height[i], height[j] 중에 작은 값, 가로는 abs(i-j) + - output = 높이 x 가로 + -> 2중 loop 는 O(n^2) 로 TLE 발생. + -> two pointer 로 O(n) 으로 해결! + ''' + + n = len(height) + maxArea = 0 + + left = 0 + right = n-1 + + while left < right: + curArea = abs(right-left) * min(height[left], height[right]) + maxArea = max(curArea, maxArea) + + #height 이 낮은쪽 pointer update + if height[left] < height[right]: + left += 1 + else: + right -= 1 + + return maxArea + \ No newline at end of file diff --git a/longest-increasing-subsequence/hyeri0903.py b/longest-increasing-subsequence/hyeri0903.py new file mode 100644 index 0000000000..f87953236a --- /dev/null +++ b/longest-increasing-subsequence/hyeri0903.py @@ -0,0 +1,26 @@ +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + ''' + 모르겠어서 풀이봤습니다 ㅜㅜ + 1.problem: 증가하는 가장 긴 subsequence length return (최장 증가 부분 수열) + 2.조건 + - nums array 길이 최소 1 , 최대 2500 + - 원소 값 음수 가능 + 3.풀이 + - dp : time complexity O(n^2) + dp[i] = i번째 원소를 마지막으로하는 LIS + dp[i] max(dp[i], dp[j]+1) 나 보다 작은 애들 중 가장 긴 LIS + 1 + ''' + + n = len(nums) + dp = [1] * (n) + + for i in range(n): + for j in range(i): + #앞 숫자 nums[j]가 지금 nums[i]보다 더 작은 경우 dp[i] update + if nums[j] < nums[i]: + dp[i] = max(dp[i], dp[j] + 1) + return max(dp) + + + \ No newline at end of file diff --git a/valid-parentheses/hyeri0903.py b/valid-parentheses/hyeri0903.py new file mode 100644 index 0000000000..fd914442b9 --- /dev/null +++ b/valid-parentheses/hyeri0903.py @@ -0,0 +1,37 @@ +class Solution: + def isValid(self, s: str) -> bool: + ''' + 문제: string s 에 대해서 valid parentheses 이면 true 아니면 false + conditions + - open brackets must be closed by the same type + - // must be closed in the correct order + - s 최소 길이 = 1, 최대 10^4 + solution + - open brackets -> st array 에 저장, close brackets -> st.pop -> check valid + - 마지막에 st array length 가 1 이상이면 return False + + - time complexity: O(n) + - space complexity: O(n) + ''' + + st = [] + + for i in range(len(s)): + if s[i] == '(' or s[i] == '{' or s[i] == '[': + st.append(s[i]) + else: + # check if st is empty + if len(st) <= 0: + return False + cur = st.pop() + if s[i] == ')' and cur != '(': + return False + if s[i] == '}' and cur != '{': + return False + if s[i] == ']' and cur != '[': + return False + if len(st) > 0: + return False + return True + + \ No newline at end of file