Skip to content

[jinah92] Week 6 #896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions container-with-most-water/jinah92.py
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions design-add-and-search-words-data-structure/jinah92.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions longest-increasing-subsequence/jinah92.py
Original file line number Diff line number Diff line change
@@ -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)
20 changes: 20 additions & 0 deletions valid-parentheses/jinah92.py
Original file line number Diff line number Diff line change
@@ -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
Loading