Skip to content

Commit e36dda3

Browse files
authored
Merge pull request #896 from jinah92/main
[jinah92] Week 6
2 parents 1ef7736 + 47d944c commit e36dda3

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

container-with-most-water/jinah92.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# O(1) spaces, O(N) times
2+
class Solution:
3+
def maxArea(self, height: List[int]) -> int:
4+
max_area = 0
5+
start, end = 0, len(height)-1
6+
7+
while start != end:
8+
# start, end 포인터에서 물의 넓이 계산 및 최대값 계산
9+
max_area = max((end-start)*min(height[start], height[end]), max_area)
10+
if height[start] < height[end]:
11+
start += 1
12+
elif height[start] >= height[end]:
13+
end -= 1
14+
15+
return max_area
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 공간복잡도 O(n): dictionary 멤버로 set을 사용
2+
# 시간복잡도 O(n*p): 삽입연산은 O(1)을 사용
3+
import re
4+
5+
class WordDictionary:
6+
7+
def __init__(self):
8+
self.dictionary = set()
9+
10+
def addWord(self, word: str) -> None:
11+
self.dictionary.add(word)
12+
13+
def search(self, word: str) -> bool:
14+
if '.' in word:
15+
pattern = re.compile(word)
16+
# O(n) times
17+
for item in self.dictionary:
18+
# O(p) times : 패턴의 길이(p)
19+
if pattern.fullmatch(item):
20+
return True
21+
return False
22+
else:
23+
return word in self.dictionary
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 공간 복잡도: O(n) => nums 길이만큼 dp 배열 길이만큼의 공간 사용
2+
# 시간 복잡도: O(n^2) => 외부 반복문은 O(N), 내부 반복문은 O(N) 시간이 소요되므로 총 O(N*N) = O(N^2) 소요
3+
class Solution:
4+
def lengthOfLIS(self, nums: List[int]) -> int:
5+
dp = [1] * len(nums)
6+
7+
for cur in range(1, len(nums)):
8+
for prev in range(cur):
9+
if nums[cur] > nums[prev]:
10+
dp[cur] = max(dp[prev]+1, dp[cur])
11+
12+
return max(dp)

valid-parentheses/jinah92.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# O(s) 공간 복잡도 : 입력문자열 s 길이에 따라 최대 s 깊이의 stack 생성
2+
# O(s) 시간 복잡도 : 문자열 s 길이에 따라 for문 반복
3+
class Solution:
4+
def isValid(self, s: str) -> bool:
5+
stack = []
6+
pairs = {'[': ']', '(': ')', '{': '}'}
7+
8+
for i, ch in enumerate(s):
9+
if ch == '(' or ch == '{' or ch == '[':
10+
stack.append(ch)
11+
else:
12+
# '(', '[', '{' 문자가 앞에 없이 ')', ']', '}' 문자가 오는 경우
13+
if not stack:
14+
return False
15+
lastCh = stack.pop()
16+
# pair가 맞지 않는 문자인 경우
17+
if pairs[lastCh] != ch:
18+
return False
19+
# stack에 값이 비지 않은 경우, pair가 맞지 않음
20+
return not stack

0 commit comments

Comments
 (0)