diff --git a/container-with-most-water/jinah92.py b/container-with-most-water/jinah92.py new file mode 100644 index 000000000..68cebab3f --- /dev/null +++ b/container-with-most-water/jinah92.py @@ -0,0 +1,15 @@ +# O(1) spaces, O(N) times +class Solution: + def maxArea(self, height: List[int]) -> int: + max_area = 0 + start, end = 0, len(height)-1 + + while start != end: + # start, end 포인터에서 물의 넓이 계산 및 최대값 계산 + max_area = max((end-start)*min(height[start], height[end]), max_area) + if height[start] < height[end]: + start += 1 + elif height[start] >= height[end]: + end -= 1 + + return max_area diff --git a/design-add-and-search-words-data-structure/jinah92.py b/design-add-and-search-words-data-structure/jinah92.py new file mode 100644 index 000000000..2995eb3f7 --- /dev/null +++ b/design-add-and-search-words-data-structure/jinah92.py @@ -0,0 +1,23 @@ +# 공간복잡도 O(n): dictionary 멤버로 set을 사용 +# 시간복잡도 O(n*p): 삽입연산은 O(1)을 사용 +import re + +class WordDictionary: + + def __init__(self): + self.dictionary = set() + + def addWord(self, word: str) -> None: + self.dictionary.add(word) + + def search(self, word: str) -> bool: + if '.' in word: + pattern = re.compile(word) + # O(n) times + for item in self.dictionary: + # O(p) times : 패턴의 길이(p) + if pattern.fullmatch(item): + return True + return False + else: + return word in self.dictionary diff --git a/longest-increasing-subsequence/jinah92.py b/longest-increasing-subsequence/jinah92.py new file mode 100644 index 000000000..a85a94b39 --- /dev/null +++ b/longest-increasing-subsequence/jinah92.py @@ -0,0 +1,12 @@ +# 공간 복잡도: O(n) => nums 길이만큼 dp 배열 길이만큼의 공간 사용 +# 시간 복잡도: O(n^2) => 외부 반복문은 O(N), 내부 반복문은 O(N) 시간이 소요되므로 총 O(N*N) = O(N^2) 소요 +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + dp = [1] * len(nums) + + for cur in range(1, len(nums)): + for prev in range(cur): + if nums[cur] > nums[prev]: + dp[cur] = max(dp[prev]+1, dp[cur]) + + return max(dp) diff --git a/valid-parentheses/jinah92.py b/valid-parentheses/jinah92.py new file mode 100644 index 000000000..0be463ec6 --- /dev/null +++ b/valid-parentheses/jinah92.py @@ -0,0 +1,20 @@ +# O(s) 공간 복잡도 : 입력문자열 s 길이에 따라 최대 s 깊이의 stack 생성 +# O(s) 시간 복잡도 : 문자열 s 길이에 따라 for문 반복 +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + pairs = {'[': ']', '(': ')', '{': '}'} + + for i, ch in enumerate(s): + if ch == '(' or ch == '{' or ch == '[': + stack.append(ch) + else: + # '(', '[', '{' 문자가 앞에 없이 ')', ']', '}' 문자가 오는 경우 + if not stack: + return False + lastCh = stack.pop() + # pair가 맞지 않는 문자인 경우 + if pairs[lastCh] != ch: + return False + # stack에 값이 비지 않은 경우, pair가 맞지 않음 + return not stack