Skip to content

Commit 573d35b

Browse files
authored
Merge pull request #1419 from river20s/main
[river20s] WEEK 06 solutions
2 parents 468e6e8 + ed58d52 commit 573d35b

File tree

4 files changed

+230
-0
lines changed

4 files changed

+230
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
# ์‹œ์ž‘๊ณผ ๋ ์„ ๋ถ„์„ ํฌ์ธํ„ฐ๋กœ ๋‘๊ณ 
4+
# ๋‘ ์„ ๋ถ„์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋„“์ด:
5+
# ๋„ˆ๋น„ = right - left
6+
# ๋†’์ด = min(height[left], height[right])
7+
# ๋„“์ด = ๋„ˆ๋น„ * ๋†’์ด์˜ ์ตœ๋Œ€ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
8+
# Time Complexity: O(n)
9+
# ๋‘ ํฌ์ธํ„ฐ๊ฐ€ ํ•œ ์นธ์”ฉ๋งŒ ์ด๋™ํ•˜๋ฉฐ ์„œ๋กœ ๋งŒ๋‚  ๋•Œ ๋ฃจํ”„ ์ข…๋ฃŒ
10+
# Space Complexity: O(1)
11+
n = len(height)
12+
left = 0 # ์™ผ์ชฝ(์‹œ์ž‘) ํฌ์ธํ„ฐ
13+
right = n - 1 # ์˜ค๋ฅธ์ชฝ(๋) ํฌ์ธํ„ฐ
14+
max_area = 0
15+
16+
while left < right:
17+
# ํ˜„์žฌ ๋†’์ด๋Š” ๋‘ ์ง์„  ์ค‘ ๋‚ฎ์€ ์ชฝ
18+
current_height = min(height[left], height[right])
19+
# ํ˜„์žฌ ๋„ˆ๋น„๋Š” ์˜ค๋ฅธ์ชฝ ์ ๊ณผ ์™ผ์ชฝ ์ ์˜ ์ฐจ
20+
current_width = right - left
21+
# ๋„“์ด = ๋†’์ด * ๋„ˆ๋น„
22+
current_area = current_height * current_width
23+
# ์ตœ๋Œ€ ๋„“์ด๋ผ๋ฉด ์—…๋ฐ์ดํŠธ
24+
max_area = max(max_area, current_area)
25+
# ํฌ์ธํ„ฐ ์ด๋™ ํ›„ ํƒ์ƒ‰
26+
# ๋‘˜ ์ค‘ ๋” ๋‚ฎ์€ ์ชฝ์˜ ํฌ์ธํ„ฐ๋ฅผ ์•ˆ์œผ๋กœ ์›€์ง์—ฌ์„œ ๋„“์ด ๊ณ„์‚ฐ
27+
# ๋” ํฐ ๋„“์ด๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ, ํฌ์ธํ„ฐ๋ฅผ ์•ˆ์œผ๋กœ ์›€์ง์ด๋ฉด ๋„ˆ๋น„๋Š” ๋ฌด์กฐ๊ฑด ๊ฐ์†Œ
28+
# ๋†’์ด๋ผ๋„ ์ฆ๊ฐ€ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ ๊ธฐ์กด ๋‚ฎ์€ ๋†’์ด๊ฐ€ ๋Š˜์–ด๋‚  ๊ฐ€๋Šฅ์„ฑ์— ๋ฐฐํŒ…
29+
# ๋‘˜์ด ๊ฐ™๋‹ค๋ฉด ์˜ค๋ฅธ์ชฝ ์ด๋™(์•„๋ฌด์ชฝ์ด๋‚˜ ๊ฐ€๋Šฅ)
30+
if height[left] < height[right]:
31+
left += 1
32+
else:
33+
right -= 1
34+
return max_area
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class TrieNode:
2+
def __init__(self):
3+
self.children = {}
4+
self.isEndOfWord = False
5+
# <<<--- ํŠธ๋ผ์ด ์ž๋ฃŒ ๊ตฌ์กฐ ์‚ฌ์šฉ์„ ์œ„ํ•œ ๊ตฌํ˜„ ---<<<
6+
# >>>----------------- ๋‹ต์•ˆ ---------------->>>
7+
class WordDictionary:
8+
9+
def __init__(self):
10+
self.root = TrieNode()
11+
12+
def addWord(self, word: str) -> None:
13+
# ์ถ”๊ฐ€: word๋ฅผ ์ž๋ฃŒ ๊ตฌ์กฐ์— ์ถ”๊ฐ€
14+
# ์ผ๋ฐ˜์ ์ธ ํŠธ๋ผ์ด ์‚ฝ์ž… ๋กœ์ง๊ณผ ๊ฐ™์Œ
15+
# Time Complexity: O(L), Space Complexity: O(L)
16+
# L์€ ์ถ”๊ฐ€ํ•  ๋‹จ์–ด ๊ธธ์ด
17+
currentNode = self.root
18+
for char in word:
19+
if char not in currentNode.children:
20+
currentNode.children[char] = TrieNode()
21+
currentNode = currentNode.children[char]
22+
currentNode.isEndOfWord = True
23+
24+
def search(self, word: str) -> bool:
25+
# ๊ฒ€์ƒ‰: ์ž๋ฃŒ ๊ตฌ์กฐ์— word์™€ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ์žˆ์œผ๋ฉด True
26+
# ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด False ๋ฐ˜ํ™˜
27+
# ์™€์ผ๋“œ์นด๋“œ '.'๋Š” ์–ด๋–ค ๊ธ€์ž๋„ ๋  ์ˆ˜ ์žˆ์Œ
28+
# Time Coplexity: O(M), Space Complexity: O(M)
29+
# M์€ ๊ฒ€์ƒ‰ํ•  ๋‹จ์–ด ๊ธธ์ด
30+
31+
def _dfs_search(node: TrieNode, index_in_search_word: int) -> bool:
32+
# ์žฌ๊ท€๋ฅผ ์œ„ํ•œ ํ—ฌํผ ํ•จ์ˆ˜
33+
# --- ๋ฒ ์ด์Šค ์ผ€์ด์Šค ---
34+
# ๊ฒ€์ƒ‰์— ๋์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ ํŠธ๋ผ์ด ๋…ธ๋“œ๊ฐ€ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ผ๋ฉด ๊ฒ€์ƒ‰ ์„ฑ๊ณต
35+
if index_in_search_word == len(word):
36+
return node.isEndOfWord
37+
38+
# ํŠธ๋ผ์ด์˜ ํ˜„์žฌ ๋…ธ๋“œ์™€ ๋น„๊ตํ•ด์•ผํ•  ๊ฒ€์ƒ‰์–ด word์˜ ๋ฌธ์ž
39+
char_to_match = word[index_in_search_word]
40+
41+
# ํ˜„์žฌ ๋ฌธ์ž๊ฐ€ ์•ŒํŒŒ๋ฒณ์ธ ๊ฒฝ์šฐ
42+
if char_to_match != ".":
43+
# ์ž์‹ ๋…ธ๋“œ์— ๋น„๊ตํ•  ๋ฌธ์ž๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
44+
if char_to_match not in node.children:
45+
return False
46+
# ๋‹ค์Œ ๋ฌธ์ž์— ๋Œ€ํ•œ ์žฌ๊ท€ ํ˜ธ์ถœ
47+
return _dfs_search(node.children[char_to_match], index_in_search_word + 1)
48+
49+
# ํ˜„์žฌ ๋ฌธ์ž๊ฐ€ '.'์ธ ๊ฒฝ์šฐ
50+
else:
51+
# ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ์— ๋Œ€ํ•ด ์žฌ๊ท€ ํ˜ธ์ถœ
52+
for child_node in node.children.values():
53+
if _dfs_search(child_node, index_in_search_word + 1):
54+
return True # ํ•˜๋‚˜๋ผ๋„ ์„ฑ๊ณตํ•˜๋ฉด True
55+
return False
56+
# ์ตœ์ดˆ ์žฌ๊ท€ ํ˜ธ์ถœ
57+
return _dfs_search(self.root, 0)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# import bisect
2+
from typing import List
3+
class Solution():
4+
def _my_bisect_left(self, a: List[int], x:int) -> int:
5+
"""
6+
์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ a์— x๋ฅผ ์‚ฝ์ž…ํ•  ๊ฐ€์žฅ ์™ผ์ชฝ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
7+
"""
8+
# x๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์ฒซ ๋ฒˆ์งธ ์›์†Œ์˜ ์ธ๋ฑ์Šค ์ฐพ์•„์ฃผ๊ธฐ
9+
# bisect.bisect_left(a, x)๋ฅผ ์ง์ ‘ ๊ตฌํ˜„
10+
low = 0 # ์‹œ์ž‘ ์ธ๋ฑ์Šค, 0์œผ๋กœ ์ดˆ๊ธฐํ™”
11+
high = len(a) # ๋ ์ธ๋ฑ์Šค, ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋กœ ์ดˆ๊ธฐํ™”
12+
13+
while low < high:
14+
mid = low + (high - low) // 2
15+
if a[mid] < x:
16+
low = mid + 1
17+
else: # a[mid] >= x
18+
high = mid
19+
return low # low๊ฐ€ ์‚ฝ์ž… ์ง€์ 
20+
21+
def lengthOfLIS(self, nums: List[int]) -> int:
22+
"""
23+
"""
24+
if not nums: # nums๊ฐ€ ๋นˆ ๋ฐฐ์—ด์ด๋ผ๋ฉด ๋จผ์ € 0 ๋ฐ˜ํ™˜
25+
return 0
26+
tails = [] # ๊ฐ ๊ธธ์ด์˜ LIS๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ์ž‘์€ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
27+
for num in nums:
28+
# num์„ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ์™ผ์ชฝ ์œ„์น˜ ์ฐพ๊ธฐ
29+
# -> tails ๋ฆฌ์ŠคํŠธ์—์„œ num ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ ์ธ๋ฑ์Šค ์ฐพ๊ธฐ
30+
# -> ๋งŒ์•ฝ tails ๋ชจ๋“  ๊ฐ’๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์ถ”๊ฐ€ํ•  ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•จ
31+
idx = self._my_bisect_left(tails, num)
32+
33+
# ๋งŒ์•ฝ idx๊ฐ€ tails ํ˜„์žฌ ๊ธธ์ด์™€ ๊ฐ™๋‹ค๋ฉด
34+
# -> num์ด tails์˜ ๋ชจ๋“  ์š”์†Œ๋ณด๋‹ค ํฌ๋‹ค
35+
# -> tails์— num "์ถ”๊ฐ€"
36+
if idx == len(tails):
37+
tails.append(num)
38+
# ๋งŒ์•ฝ idx๊ฐ€ tails ํ˜„์žฌ ๊ธธ์ด๋ณด๋‹ค ์ž‘๋‹ค๋ฉด
39+
# -> ๊ฐ€๋Šฅ์„ฑ ๋ฐœ๊ฒฌ, tails[idx]๋ฅผ num์œผ๋กœ "๊ต์ฒด"
40+
# -> LIS๋ฅผ ๊ฐœ์„ 
41+
else:
42+
tails[idx] = num
43+
44+
# ๋ชจ๋“  ์ˆซ์ž ์ฒ˜๋ฆฌ ํ›„, tails ๊ธธ์ด๊ฐ€ LIS๊ธธ์ด๊ฐ€ ๋จ
45+
return len(tails)
46+
47+
48+
"""
49+
def lengthOfLIS_bruteforce(self, nums: List[int]) -> int:
50+
# ์ดˆ๊ธฐ์— ๊ตฌํ˜„ํ•œ ํƒ์š• ๊ธฐ๋ฐ˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜
51+
# --- ๋ณต์žก๋„ ---
52+
# Time Complexity: O(n^2)
53+
# Space Complexity: O(1)
54+
# --- ํ•œ๊ณ„ ---
55+
# [์ง€๊ธˆ ๋‹น์žฅ ๊ฐ€์žฅ ์ข‹์•„๋ณด์ด๋Š” ์„ ํƒ์ด ์ „์ฒด์ ์œผ๋กœ๋Š” ์ตœ์ ์ด ์•„๋‹Œ ๊ฒฝ์šฐ]
56+
# nums = [0,1,0,3,2,3] ๊ฐ™์€ ๊ฒฝ์šฐ ๋‹ต์„ ์ฐพ์ง€ ๋ชปํ•จ (์˜ˆ์ธก ๊ฐ’: 4, ์‹ค์ œ ๊ฐ’: 3)
57+
# ๊ฐ ์š”์†Œ์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ํƒ์š•์ ์œผ๋กœ ๋‹ค์Œ ํฐ ์ˆ˜๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ชจ๋“  LIS๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ
58+
# ๊ฐ ์‹œ์ž‘์ ์—์„œ ํ•˜๋‚˜์˜ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด์„ ์ฐพ๋Š” ๊ฒƒ, ํ•ญ์ƒ ๊ธด ๊ฒƒ์„ ๋ณด์žฅํ•  ์ˆ˜ ์—†์Œ
59+
# ์ด๋ฏธ ์ฐพ์€ LIS ๋’ค์— ๋” ๊ธด LIS๊ฐ€ ์žˆ๋‹ค๋ฉด ์ฐพ์„ ์ˆ˜ ์—†์Œ
60+
n = len(nums) # n์€ nums์˜ ๊ธธ์ด(๊ฐœ์ˆ˜)
61+
if n == 0: # ๊ธธ์ด๊ฐ€ 0์ด๋ฉด ์šฐ์„ ์ ์œผ๋กœ 0์„ ๋ฐ˜ํ™˜
62+
return 0
63+
64+
max_overall_length = 0 # ์ „์ฒด ์ตœ๋Œ€ ๊ธธ์ด, 0์œผ๋กœ ์ดˆ๊ธฐํ™”
65+
66+
for idx_i in range(n): # 0๋ถ€ํ„ฐ n๊นŒ์ง€ ์ˆœํšŒํ•˜๋Š” ์™ธ๋ถ€ ๋ฃจํ”„
67+
current_length = 1 # ํ˜„์žฌ(index: idx_i) ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๊ธธ์ด๋ฅผ 1๋กœ ์ดˆ๊ธฐํ™”
68+
last_element_in_subsequence = nums[idx_i] # ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๋งˆ์ง€๋ง‰ ์ˆซ์ž๋ฅผ ํ˜„์žฌ ์ˆซ์ž๋กœ ์ดˆ๊ธฐํ™”
69+
70+
for idx_j in range(idx_i + 1, n): # ๋‹ค์Œ ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ idx_j๋กœ ํ•˜์—ฌ ๋๊นŒ์ง€ ์ˆœํšŒ
71+
# ๋งŒ์•ฝ ๋‹ค์Œ ์š”์†Œ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๋งˆ์ง€๋ง‰ ์ˆซ์ž๋ณด๋‹ค ํฌ๋‹ค๋ฉด
72+
if nums[idx_j] > last_element_in_subsequence:
73+
# ๋งˆ์ง€๋ง‰ ์ˆซ์ž๋ฅผ ๋‹ค์Œ ์š”์†Œ๋กœ ๊ฐฑ์‹ 
74+
last_element_in_subsequence = nums[idx_j]
75+
# ํ˜„์žฌ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๊ธธ์ด๋ฅผ 1 ์ฆ๊ฐ€
76+
current_length += 1
77+
78+
# ํ˜„์žฌ ๋ถ€๋ถ„ ์ˆ˜์—ด ๊ธธ์ด๊ฐ€ ์ „์ฒด ๋ถ€๋ถ„ ์ˆ˜์—ด ๊ธธ์ด๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ ๊ฐฑ์‹ 
79+
max_overall_length = max(max_overall_length, current_length)
80+
81+
return max_overall_length
82+
"""

โ€Žvalid-parentheses/river20s.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class Stack:
2+
def __init__(self):
3+
self.data = []
4+
5+
def is_empty(self):
6+
return len(self.data) == 0
7+
8+
def push(self, element):
9+
self.data.append(element)
10+
11+
def pop(self):
12+
if not self.is_empty():
13+
return self.data.pop()
14+
else:
15+
return None
16+
17+
def peek(self):
18+
if not self.is_empty():
19+
return self.data[-1]
20+
else:
21+
return None
22+
# <<<--- Stack ๊ตฌํ˜„ ---<<<
23+
# >>>--- ๋‹ต์•ˆ Solution --->>>
24+
class Solution:
25+
# ์Šคํƒ์„ ํ™œ์šฉํ•ด ๊ด„ํ˜ธ ์œ ํšจ ๊ฒ€์‚ฌ
26+
# Time Complexity: O(n)
27+
# Space Complexity: O(n)
28+
def __init__(self):
29+
self._opening_brackets = '([{'
30+
self._closing_brackets = ')]}'
31+
self._matching_map = {')': '(', ']': '[', '}': '{'}
32+
33+
def isValid(self, s: str) -> bool:
34+
stack = Stack()
35+
for char in s:
36+
# ์—ฌ๋Š” ๊ด„ํ˜ธ๋ผ๋ฉด ์Šคํƒ์— push
37+
if self._is_opening(char):
38+
stack.push(char)
39+
# ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ผ๋ฉด
40+
# ๋งˆ์ง€๋ง‰ ์—ด๋ฆฐ ๊ด„ํ˜ธ์™€ ์œ ํ˜• ์ผ์น˜ ํ™•์ธ
41+
elif self._is_closing(char):
42+
if stack.is_empty():
43+
# ์Šคํƒ์ด ๋น„์–ด ์žˆ์œผ๋ฉด False ๋ฐ˜ํ™˜
44+
return False
45+
last_open_bracket = stack.pop()
46+
if not self._is_match(last_open_bracket, char):
47+
return False
48+
return stack.is_empty()
49+
50+
def _is_opening(self, char: str) -> bool:
51+
return char in self._opening_brackets
52+
53+
def _is_closing(self, char: str) -> bool:
54+
return char in self._closing_brackets
55+
56+
def _is_match(self, open_bracket: str, close_bracket: str) -> bool:
57+
return self._matching_map.get(close_bracket) == open_bracket

0 commit comments

Comments
ย (0)