Skip to content

Commit 6564843

Browse files
authored
Merge pull request #1869 from hu6r1s/main
[hu6r1s] WEEK 06 Solutions
2 parents a61ca49 + ca90209 commit 6564843

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
class Solution:
3+
"""
4+
1. ๋ธŒ๋ฃจํŠธํฌ์Šค์™€ ๊ฐ™์ด ์ „๋ถ€ ๊ธธ์ด๋ฅผ ๋Œ€์กฐํ•ด๋ณด๊ณ  ํ•˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ํ„ฐ์งˆ ๊ฒƒ.
5+
ํˆฌํฌ์ธํ„ฐ ๋ฐฉ์‹์„ ํ™œ์šฉํ•˜๋ฉด ๋จ. ํˆฌํฌ์ธํ„ฐ์— ๋Œ€ํ•œ ๋ฌธ์ œ๊ฐ€ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋กœ ๋งŽ์ด ๋‚˜์˜ฌ ๊ฒƒ ๊ฐ™์Œ.
6+
ํ™•์‹คํ•˜๊ฒŒ ๊ณต๋ถ€ ํ•„์š”.
7+
"""
8+
def maxArea(self, height: List[int]) -> int:
9+
max_area = 0
10+
start, end = 0, len(height) - 1
11+
while start < end:
12+
area = (end - start) * min(height[start], height[end])
13+
max_area = max(area, max_area)
14+
15+
if height[start] <= height[end]:
16+
start += 1
17+
else:
18+
end -= 1
19+
20+
return max_area
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class WordDictionary:
2+
"""
3+
์•Œ๊ณ ๋‹ฌ๋ ˆ์˜ ์˜์ƒ์„ ๋ณด๊ณ  ํ–ˆ๋Š”๋ฐ ์•„์ง ์ดํ•ด๊ฐ€ ์ž˜ ๋˜์ง€ ์•Š์Œ.
4+
์ถ”๊ฐ€์ ์ธ ํ•™์Šต ํ•„์š”
5+
"""
6+
def __init__(self):
7+
self.root = {"$": True}
8+
9+
def addWord(self, word: str) -> None:
10+
node = self.root
11+
for ch in word:
12+
if ch not in node:
13+
node[ch] = {"$": False}
14+
node = node[ch]
15+
node["$"] = True
16+
17+
def search(self, word: str) -> bool:
18+
def dfs(node, idx):
19+
if idx == len(word):
20+
return node["$"]
21+
ch = word[idx]
22+
if ch in node:
23+
return dfs(node[ch], idx + 1)
24+
if ch == ".":
25+
if any(dfs(node[k], idx + 1) for k in node if k != "$"):
26+
return True
27+
return False
28+
29+
return dfs(self.root, 0)
30+
31+
32+
# Your WordDictionary object will be instantiated and called as such:
33+
# obj = WordDictionary()
34+
# obj.addWord(word)
35+
# param_2 = obj.search(word)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from bisect import bisect_left
2+
3+
class Solution:
4+
"""
5+
๋ญ”๊ฐ€ bfs ํ’€์ด ๋ฐฉ์‹
6+
๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ
7+
"""
8+
# def lengthOfLIS(self, nums: List[int]) -> int:
9+
# n = len(nums)
10+
# q = deque()
11+
# q.append((-1, float('-inf'), 0)) # (idx, lastValue, length)
12+
# answer = 0
13+
14+
# while q:
15+
# idx, last, length = q.popleft()
16+
# answer = max(answer, length)
17+
18+
# for nxt in range(idx + 1, n):
19+
# if nums[nxt] > last:
20+
# q.append((nxt, nums[nxt], length + 1))
21+
22+
# return answer
23+
24+
# def lengthOfLIS(self, nums: List[int]) -> int:
25+
# dp = [1] * len(nums)
26+
# for cur in range(1, len(nums)):
27+
# for pre in range(cur):
28+
# if nums[pre] < nums[cur]:
29+
# dp[cur] = max(1 + dp[pre], dp[cur])
30+
# return max(dp)
31+
32+
def lengthOfLIS(self, nums: List[int]) -> int:
33+
sub = []
34+
for num in nums:
35+
index = bisect_left(sub, num)
36+
if index == len(sub):
37+
sub.append(num)
38+
else:
39+
sub[index] = num
40+
return len(sub)

โ€Žvalid-parentheses/hu6r1s.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
"""
3+
1. ์Šคํƒ์„ ํ™œ์šฉํ•œ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ
4+
์—ฌ๋Š” ๊ด„ํ˜ธ์ผ ๋•Œ๋Š” ์Šคํƒ์— ๋ฌด์กฐ๊ฑด ๋„ฃ์–ด์ค€๋‹ค.
5+
๋‹ซ๋Š” ๊ด„ํ˜ธ์ผ ๋•Œ๋Š” ๋Œ€, ์ค‘, ์†Œ ๊ด„ํ˜ธ์— ๋งž์ถฐ์„œ ๋ถ„๊ธฐ๋ฅผ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.
6+
์Šคํƒ์ด ์žˆ๊ณ , ์Šคํƒ์˜ ๋งˆ์ง€๋ง‰์ด ํ•ด๋‹น ๊ด„ํ˜ธ์˜ ์—ฌ๋Š” ๊ด„ํ˜ธ์ด๋ฉด ๋นผ๋‚ด์ค€๋‹ค.
7+
์ด์™ธ๋Š” ๋‹ซํžŒ ๊ด„ํ˜ธ๊ฐ€ ๋จผ์ € ๋‚˜์˜ค๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— False๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.
8+
์ „ํ˜•์ ์ธ ์Šคํƒ ๋ฌธ์ œ๋กœ O(n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.
9+
"""
10+
def isValid(self, s: str) -> bool:
11+
stack = []
12+
for word in s:
13+
if word == "(" or word == "{" or word == "[":
14+
stack.append(word)
15+
elif stack and word == ")" and stack[-1] == "(":
16+
stack.pop()
17+
elif stack and word == "]" and stack[-1] == "[":
18+
stack.pop()
19+
elif stack and word == "}" and stack[-1] == "{":
20+
stack.pop()
21+
else:
22+
return False
23+
return True if not stack else False

0 commit comments

Comments
ย (0)