Skip to content

Commit a41c907

Browse files
authored
Merge pull request DaleStudy#888 from jungsiroo/main
[jungsiroo] Week 06
2 parents 817f6f0 + 508129a commit a41c907

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
# Naive - ์™„์ „ํƒ์ƒ‰
4+
n = len(height)
5+
6+
"""
7+
๋ชจ๋“  ์กฐํ•ฉ์„ ๊ณ„์‚ฐํ•œ ํ›„ ์ตœ๋Œ“๊ฐ’ ๋ฆฌํ„ด
8+
Tc : O(n^2)
9+
Sc : O(1)
10+
11+
ret = 0
12+
13+
for i in range(n-1):
14+
for j in range(i+1, n):
15+
w = j - i
16+
h = min(height[i], height[j])
17+
ret = max(ret, w*h)
18+
return ret
19+
"""
20+
21+
# Better Solution
22+
"""
23+
ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•œ ๋ฐฉ๋ฒ•
24+
ํฌ์ธํ„ฐ๋ฅผ ์›€์ง์ผ ๋•Œ๋Š” ๋” ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง„ ์ชฝ์ด ํ•œ์นธ ์›€์ง์—ฌ ๊ทธ ๋†’์ด๋ฅผ ๋†’์ด๋Š” ๋ฐฉํ–ฅ ์ชฝ์œผ๋กœ ์ง„ํ–‰
25+
26+
Tc : O(n)
27+
Sc : O(1)
28+
29+
"""
30+
left, right = 0, n-1
31+
ret = 0
32+
33+
while left < right:
34+
w = right - left
35+
h = min(height[left], height[right])
36+
ret = max(ret, h*w)
37+
38+
if height[left] <= height[right]:
39+
left += 1
40+
else:
41+
right -= 1
42+
return ret
43+
44+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Node:
2+
def __init__(self, char=None):
3+
self.is_end = False
4+
self.children = {}
5+
6+
class WordDictionary:
7+
"""
8+
ํŠธ๋ผ์ด๋ฅผ ํ™œ์šฉ
9+
๊ธฐ๋ณธ ๋ฌธ์ œ์™€ ๋‹ค๋ฅธ ์ ์€ search ํ•  ๋•Œ "." ์ด ํฌํ•จ๋˜์–ด์žˆ๋Š” ๊ฒƒ
10+
11+
๋”ฐ๋ผ์„œ . ์ด๋ผ๋ฉด ํ•ด๋‹น ๋…ธ๋“œ์˜ ๋ชจ๋“  ์ž์‹์„ ๋ชจ๋‘ ์กฐํšŒํ•ด์•ผ๋จ
12+
13+
addWord
14+
w = len(word)
15+
Tc : O(w)
16+
Sc : O(w)
17+
18+
search
19+
์ตœ์•…์˜ ๊ฒฝ์šฐ case : xa, xb, xc, xd, .... xz ๋กœ x์˜ ๋‹ค์Œ ๊ธ€์ž๊ฐ€ a~z์ผ ๋•Œ
20+
search("x.") ๋ผ๋ฉด 26๊ฐœ ๋ชจ๋‘๋ฅผ ์ฐพ์•„๋ด์•ผ๋จ
21+
Tc : O(26^w)
22+
Sc : O(w) (๊ธ€์ž ๊ธธ์ด๋งŒํผ ์žฌ๊ท€๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ์—)
23+
24+
"""
25+
def __init__(self):
26+
self.root = Node()
27+
28+
def addWord(self, word: str) -> None:
29+
now = self.root
30+
31+
for ch in word:
32+
if ch not in now.children:
33+
now.children[ch] = Node(ch)
34+
now = now.children[ch]
35+
now.is_end = True
36+
37+
def search(self, word: str) -> bool:
38+
def _recur_search(node, index):
39+
if index == len(word):
40+
return node.is_end
41+
42+
if word[index] == ".":
43+
for ch in node.children:
44+
if _recur_search(node.children[ch], index+1):
45+
return True
46+
47+
if word[index] in node.children:
48+
return _recur_search(node.children[word[index]], index+1)
49+
return False
50+
51+
return _recur_search(self.root, 0)
52+
53+
54+
55+
56+
# Your WordDictionary object will be instantiated and called as such:
57+
# obj = WordDictionary()
58+
# obj.addWord(word)
59+
# param_2 = obj.search(word)
60+

โ€Žspiral-matrix/jungsiroo.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from collections import deque
2+
3+
"""
4+
BFS์˜ ์•„์ด๋””์–ด๋ฅผ ์ฐจ์šฉํ•˜์—ฌ ํ’€์ด
5+
6+
๋์—์„œ๋ถ€ํ„ฐ ์•ˆ์ชฝ์œผ๋กœ ๋Œ์•„๊ฐ€์•ผํ•˜๊ธฐ์— ๋ฏธ๋ฆฌ ๋Œ์•„๊ฐˆ ๋ฐฉํ–ฅ ์ง€์ • : (dx, dy)
7+
ํ•œ์นธ์”ฉ ์ด๋™ํ•ด๊ฐ€๋ฉด์„œ ๋ฒ”์œ„ ๋ฐ–์„ ๋„˜์–ด๊ฐ”๊ฑฐ๋‚˜ ์ด๋ฏธ visitํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐœ๊ฒฌ๋˜๋ฉด ๋ฐฉํ–ฅ์„ ๊บพ์Œ
8+
9+
Tc : O(m*n)
10+
Sc : O(m*n)
11+
"""
12+
13+
# r, d, l ,u
14+
dx = [0,1,0,-1]
15+
dy = [1,0,-1,0]
16+
17+
class Solution:
18+
def __init__(self):
19+
self.m = 0
20+
self.n = 0
21+
22+
def in_range(self, r, c):
23+
if r<0 or r>=self.m or c<0 or c>=self.n:
24+
return False
25+
return True
26+
27+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
28+
INF = int(1e9)
29+
d = 0
30+
self.m, self.n = len(matrix), len(matrix[0])
31+
r, c = 0, 0
32+
33+
ret = []
34+
35+
for _ in range(self.m * self.n):
36+
ret.append(matrix[r][c])
37+
if not self.in_range(r+dx[d], c+dy[d]) or matrix[r+dx[d]][c+dy[d]] == INF:
38+
d = (d+1)%4
39+
matrix[r][c] = INF
40+
r, c = r+dx[d], c+dy[d]
41+
42+
return ret
43+
44+

โ€Žvalid-parentheses/jungsiroo.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
"""
4+
์Šคํƒ์„ ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ ํ’€์ด
5+
6+
s์— ํฌํ•จ๋  ๊ด„ํ˜ธ๋“ค์€ ์ด๋ฏธ ์ •ํ•ด์ ธ์žˆ๋Š” ์ƒํƒœ์ด๊ธฐ์— ๋”•์…”๋„ˆ๋ฆฌ ์ด์šฉ
7+
์ด๋ฅผ ์ด์šฉํ•ด stack์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๊ฐ€ ํ˜„์žฌ ๊ด„ํ˜ธ์™€ ๋งค์น˜๋˜๋Š”์ง€๋ฅผ ์ •์ˆ˜๋ฅผ ํ†ตํ•ด ์•Œ ์ˆ˜ ์žˆ์Œ
8+
9+
๋งˆ์ง€๋ง‰์€ stack์ด ์ „๋ถ€ ๋น„์—ˆ์„ ๋•Œ True๋ฅผ ๋ฆฌํ„ด
10+
11+
Time Complexity : O(n)
12+
Space Complexity : O(n) (stack ๋ณ€์ˆ˜)
13+
"""
14+
15+
16+
chars = {'(':1, '{':2, '[':3, ')':-1, '}':-2, ']':-3}
17+
stack = []
18+
19+
for char in s:
20+
if chars[char] > 0:
21+
stack.append(char)
22+
else:
23+
if not stack : return False
24+
25+
if chars[stack[-1]] == -chars[char]:
26+
stack.pop()
27+
else:
28+
return False
29+
30+
return not stack
31+
32+

0 commit comments

Comments
ย (0)