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
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)