Skip to content

Commit 1ea1795

Browse files
authored
Merge pull request #1431 from yyyyyyyyyKim/main
[yyyyyyyyyKim] WEEK 06 solutions
2 parents 97b7dc8 + 52a2ff4 commit 1ea1795

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
4+
# ํˆฌํฌ์ธํ„ฐ(two pointer) : ์‹œ๊ฐ„๋ณต์žก๋„ O(n)
5+
left, right = 0, len(height)-1
6+
answer = 0
7+
8+
while left < right:
9+
width = right - left # ์ธ๋ฑ์Šค ์ฐจ์ด๊ฐ€ ๋‘ ๋ฒฝ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ(๋„ˆ๋น„)
10+
h = min(height[left], height[right]) # ๋” ๋‚ฎ์€ ๋ฒฝ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌผ์„ ์ฑ„์šธ ์ˆ˜ ์žˆ์Œ(๋†’์ด)
11+
12+
answer = max(answer, width * h) # ์ตœ๋Œ€ ๋„“์ด ์—…๋ฐ์ดํŠธ
13+
14+
# ํฌ์ธํ„ฐ ์ด๋™(๋” ๋‚ฎ์€ ๊ฐ’์„ ๊ฐ€์ง„ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™)
15+
if height[left] < height[right]:
16+
left += 1
17+
else:
18+
right -= 1
19+
20+
return answer
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
TrieNode: ํ•œ ๊ธ€์ž๋ฅผ ๋‹ด๋Š” ๋…ธ๋“œ
3+
- children: ์ž์‹๋…ธ๋“œ๋“ค ์ €์žฅํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ
4+
- end: ํ•ด๋‹น ๋…ธ๋“œ์—์„œ ๋‹จ์–ด๊ฐ€ ๋๋‚˜๋Š”์ง€ ์—ฌ๋ถ€
5+
"""
6+
class TrieNode:
7+
# Trie ๋…ธ๋“œ ์ดˆ๊ธฐํ™”
8+
def __init__(self):
9+
self.children = {} # ์ž์‹ ๋…ธ๋“œ ์ €์žฅ
10+
self.end = False # ๋‹จ์–ด์˜ ๋์ธ์ง€ ํ‘œ์‹œ
11+
12+
"""
13+
Trie(ํŠธ๋ผ์ด)๊ตฌ์กฐ, DFS, ์žฌ๊ท€
14+
- addWord : ๋‹จ์–ด๋ฅผ Trie์— ์‚ฝ์ž…
15+
- search : ๋‹จ์–ด๊ฐ€ Trie์— ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์ƒ‰
16+
"""
17+
class WordDictionary:
18+
19+
# Trie ์ž๋ฃŒ๊ตฌ์กฐ ์ดˆ๊ธฐํ™”(๋ฃจํŠธ ๋…ธ๋“œ ์ƒ์„ฑ)
20+
def __init__(self):
21+
# Trie์˜ ์‹œ์ž‘(๋นˆ ๋ฃจํŠธ ๋…ธ๋“œ ์ƒ์„ฑ)
22+
self.root = TrieNode()
23+
24+
def addWord(self, word: str) -> None:
25+
node = self.root # ๋‹จ์–ด ์‚ฝ์ž…์˜ ์‹œ์ž‘ ์œ„์น˜ = ๋ฃจํŠธ ๋…ธ๋“œ
26+
27+
# ๋‹จ์–ด์˜ ๊ธ€์ž ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ Trie์— ์‚ฝ์ž…
28+
for i in word:
29+
# ๊ธ€์ž๊ฐ€ ์ž์‹ ๋…ธ๋“œ์— ์—†์œผ๋ฉด ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์ƒ์„ฑํ•ด์„œ ๋ป—์–ด๊ฐ€๊ธฐ
30+
if i not in node.children:
31+
node.children[i] = TrieNode()
32+
33+
node = node.children[i] # ๋‹ค์Œ ๊ธ€์ž(๋…ธ๋“œ)๋กœ ์ด๋™
34+
35+
node.end = True # ๋‹จ์–ด ์‚ฝ์ž… ์™„๋ฃŒ, ํ˜„์žฌ ๋…ธ๋“œ์—์„œ ๋‹จ์–ด์˜ ๋ ํ‘œ์‹œ(True)
36+
37+
38+
def search(self, word: str) -> bool:
39+
40+
def dfs(node,i):
41+
# ๋‹จ์–ด๋ฅผ ๋‹ค ๋Œ๊ณ  ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๊ฐ€ ๋‹จ์–ด์˜ ๋์ด๋ฉด node.end๋Š” True
42+
if i == len(word):
43+
return node.end
44+
45+
if word[i] == ".":
46+
# "." ์ผ ๊ฒฝ์šฐ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ ๋Œ€์ƒ์œผ๋กœ ์žฌ๊ท€ ํƒ์ƒ‰
47+
for j in node.children.values():
48+
if dfs(j, i+1):
49+
return True
50+
else:
51+
# "."์ด ์•„๋‹Œ ๊ฒฝ์šฐ ์ž์‹๋…ธ๋“œ์— ์žˆ๋Š”์ง€ ํ™•์ธ.
52+
# ์žˆ์œผ๋ฉด ๋‹ค์Œ ๊ธ€์ž ํƒ์ƒ‰, ์—†์œผ๋ฉด False ๋ฆฌํ„ด
53+
if word[i] in node.children:
54+
return dfs(node.children[word[i]], i+1)
55+
else:
56+
return False
57+
58+
# ์ผ์น˜ํ•˜๋Š” ๋‹จ์–ด๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์— ๋Œ€๋น„ํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
59+
return False
60+
61+
# DFS์‹œ์ž‘(๋ฃจํŠธ ๋…ธ๋“œ๋ถ€ํ„ฐ ํƒ์ƒ‰)
62+
return dfs(self.root, 0)
63+
64+
# Your WordDictionary object will be instantiated and called as such:
65+
# obj = WordDictionary()
66+
# obj.addWord(word)
67+
# param_2 = obj.search(word)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def lengthOfLIS(self, nums: List[int]) -> int:
3+
4+
# DP(๊ฐ ์ธ๋ฑ์Šค๋ฅผ ๋์œผ๋กœ ํ•˜๋Š” LIS์˜ ์ตœ๋Œ€ ๊ธธ์ด ์ €์žฅ)
5+
dp = [1]*len(nums) # ์ดˆ๊ธฐ๊ฐ’์€ 1(์ž์‹  ํ•˜๋‚˜๋งŒ ์žˆ์„ ๊ฒฝ์šฐ)
6+
7+
for i in range(len(nums)):
8+
for j in range(i):
9+
# ํ˜„์žฌ๊ฐ’(nums[i])์ด ์ด์ „๊ฐ’(nums[j])๋ณด๋‹ค ํฌ๋ฉด dp[i]์—…๋ฐ์ดํŠธ
10+
if nums[i] > nums[j]:
11+
dp[i] = max(dp[i],dp[j]+1)
12+
13+
# dp์— ์ €์žฅ๋œ ์ตœ๋Œ€๊ฐ’ ๋ฆฌํ„ด
14+
return max(dp)
15+
16+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n^2), ๊ณต๊ฐ„๋ณต์žก๋„ O(n)

โ€Žspiral-matrix/yyyyyyyyyKim.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
3+
4+
answer = []
5+
6+
# ๊ฒฝ๊ณ„๊ฐ’
7+
top, bottom = 0, len(matrix)-1
8+
left, right = 0, len(matrix[0])-1
9+
10+
# ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ๋ฐ”ํ€ด์”ฉ ๋Œ๊ธฐ
11+
while top <= bottom and left <= right:
12+
13+
# ์˜ค๋ฅธ์ชฝ ์ด๋™
14+
for i in range(left,right+1):
15+
answer.append(matrix[top][i])
16+
top += 1
17+
18+
# ์•„๋ž˜๋กœ ์ด๋™
19+
for i in range(top,bottom+1):
20+
answer.append(matrix[i][right])
21+
right -= 1
22+
23+
# ์™ผ์ชฝ ์ด๋™
24+
if top <= bottom: # ์œ„์—์„œ top์„ ์ฆ๊ฐ€์‹œ์ผฐ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ๊ฒ€์‚ฌ(์•„์ง ์•„๋ž˜์ชฝ์— ํ–‰์ด ๋‚จ์•„์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ˆ˜ํ–‰)
25+
for i in range(right,left-1,-1):
26+
answer.append(matrix[bottom][i])
27+
bottom -= 1
28+
29+
# ์œ„๋กœ ์ด๋™
30+
if left <= right: # ์œ„์—์„œ right๋ฅผ ๊ฐ์†Œ์‹œ์ผฐ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ๊ฒ€์‚ฌ(์•„์ง ์™ผ์ชฝ์— ์—ด์ด ๋‚จ์•„์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ˆ˜ํ–‰)
31+
for i in range(bottom,top-1,-1):
32+
answer.append(matrix[i][left])
33+
left += 1
34+
35+
return answer

โ€Žvalid-parentheses/yyyyyyyyyKim.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
4+
# Stack
5+
stack = []
6+
i = 0
7+
8+
while i < len(s):
9+
# ์—ฌ๋Š” ๊ด„ํ˜ธ์ผ ๊ฒฝ์šฐ ์Šคํƒ์— ์ถ”๊ฐ€
10+
if s[i] == "(" or s[i] == "{" or s[i] == "[":
11+
stack.append(s[i])
12+
13+
# ๋‹ซ๋Š” ๊ด„ํ˜ธ์ผ ๊ฒฝ์šฐ
14+
else:
15+
# ์Šคํƒ์ด ๋น„์–ด ์žˆ์„๊ฒฝ์šฐ ์ง์ด ๋งž์ง€์•Š์œผ๋ฏ€๋กœ False ๋ฆฌํ„ด
16+
if not stack:
17+
return False
18+
19+
# ์Šคํƒ ๋งˆ์ง€๋ง‰์— ์ง๋งž๋Š” ์—ฌ๋Š” ๊ด„ํ˜ธ๊ฐ€ ์—†์œผ๋ฉด False
20+
if s[i] == ")" and stack[-1] != "(":
21+
return False
22+
elif s[i] == "}" and stack[-1] != "{":
23+
return False
24+
elif s[i] == "]" and stack[-1] != "[":
25+
return False
26+
27+
# ์ง์ด ๋งž์œผ๋ฉด pop
28+
stack.pop()
29+
30+
# ๋‹ค์Œ ๊ธ€์ž๋กœ ์ด๋™
31+
i += 1
32+
33+
# ๋ฌธ์ž์—ด์„ ๋‹ค ๋Œ๊ณ , ์Šคํƒ๋„ ๋น„์–ด์žˆ๋‹ค๋ฉด True ๋ฆฌํ„ด
34+
return not stack

0 commit comments

Comments
ย (0)