Skip to content

Commit 16e6492

Browse files
authored
Merge pull request #897 from Chaedie/main
[Chaedie] Week 6
2 parents 132b69e + 6eccf0f commit 16e6492

File tree

6 files changed

+361
-0
lines changed

6 files changed

+361
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Solution: Brute Force
3+
Time: O(n^2)
4+
Space: O(1)
5+
"""
6+
7+
8+
class Solution:
9+
def maxArea(self, height: List[int]) -> int:
10+
max_water = 0
11+
for i in range(len(height) - 1):
12+
for j in range(i + 1, len(height)):
13+
h = min(height[i], height[j])
14+
w = j - i
15+
max_water = max(h * w, max_water)
16+
17+
return max_water
18+
19+
20+
"""
21+
Solution: Two Pointer
22+
1) ํฌ์ธํ„ฐ ์ด๋™์€ left height, right height ์ค‘ ์ž‘์€ value๋ฅผ ๊ฐ€์งˆ ๊ฒฝ์šฐ ์ด๋™
23+
Time: O(n)
24+
Space: O(1)
25+
"""
26+
27+
28+
class Solution:
29+
def maxArea(self, height: List[int]) -> int:
30+
l = 0
31+
r = len(height) - 1
32+
max_water = 0
33+
34+
while l < r:
35+
h = min(height[l], height[r])
36+
w = r - l
37+
max_water = max(h * w, max_water)
38+
39+
if height[l] < height[r]:
40+
l += 1
41+
else:
42+
r -= 1
43+
return max_water
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""
2+
ํ’€์ด๋ฅผ ๋ณด๊ณ  ๊ณต๋ถ€ํ–ˆ์Šต๋‹ˆ๋‹ค.
3+
4+
Solution:
5+
set์„ ์ด์šฉ,
6+
1) addWord ์—์„  add
7+
2) search ์—์„  .์„ ํฌํ•จํ•œ ๋ชจ๋“  ๊ธ€์ž๊ฐ€ ๋™์ผํ•œ ๋‹จ์–ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
8+
9+
search
10+
Time: O(n * w)
11+
Space: O(1)
12+
"""
13+
14+
15+
class WordDictionary:
16+
17+
def __init__(self):
18+
self.root = set()
19+
20+
def addWord(self, word: str) -> None:
21+
self.root.add(word)
22+
23+
def search(self, word: str) -> bool:
24+
for candidate in self.root:
25+
if len(candidate) != len(word):
26+
continue
27+
if all(w == c or w == "." for w, c in zip(word, candidate)):
28+
return True
29+
return False
30+
31+
32+
"""
33+
ํ’€์ด๋ฅผ ๋ณด๊ณ  ๊ณต๋ถ€ํ–ˆ์Šต๋‹ˆ๋‹ค.
34+
35+
Solution: Trie - ๋‹ฌ๋ ˆ์˜ ์ฝ”๋“œ
36+
"""
37+
38+
39+
class WordDictionary:
40+
41+
def __init__(self):
42+
self.root = {"$": True}
43+
44+
def addWord(self, word: str) -> None:
45+
node = self.root
46+
for ch in word:
47+
if ch not in node:
48+
node[ch] = {"$": False}
49+
node = node[ch]
50+
node["$"] = True
51+
52+
def search(self, word: str) -> bool:
53+
def dfs(node, idx):
54+
if idx == len(word):
55+
return node["$"]
56+
57+
ch = word[idx]
58+
if ch in node:
59+
return dfs(node[ch], idx + 1)
60+
if ch == ".":
61+
if any(dfs(node[k], idx + 1) for k in node if k != "$"):
62+
return True
63+
return False
64+
65+
return dfs(self.root, 0)
66+
67+
68+
"""
69+
ํ’€์ด๋ฅผ ๋ณด๊ณ  ๊ณต๋ถ€ํ–ˆ์Šต๋‹ˆ๋‹ค.
70+
71+
Solution: Trie with TrieNode - NeetCode
72+
"""
73+
74+
75+
class TrieNode:
76+
def __init__(self):
77+
self.children = {} # a: TrieNode
78+
self.word = False
79+
80+
81+
class WordDictionary:
82+
83+
def __init__(self):
84+
self.root = TrieNode()
85+
86+
def addWord(self, word: str) -> None:
87+
cur = self.root
88+
89+
for c in word:
90+
if c not in cur.children:
91+
cur.children[c] = TrieNode()
92+
cur = cur.children[c]
93+
cur.word = True
94+
95+
def search(self, word: str) -> bool:
96+
def dfs(j, root):
97+
cur = root
98+
for i in range(j, len(word)):
99+
c = word[i]
100+
101+
if c == ".":
102+
for child in cur.children.values():
103+
if dfs(i + 1, child):
104+
return True
105+
return False
106+
else:
107+
if c not in cur.children:
108+
return False
109+
cur = cur.children[c]
110+
return cur.word
111+
112+
return dfs(0, self.root)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Solution:
3+
1) encode: ๊ฐ ๊ธ€์ž์˜ ์•ž์— ๊ธ€์ž์ˆ˜์™€ # ๋ผ๋Š” delimiter ๋ฅผ ๋ถ™์—ฌ stringify ํ•œ๋‹ค.
4+
2) decode: length ๋ฅผ ์ฐธ๊ณ ์‚ผ์•„ word๋ฅผ ๋”ฐ๋‚ด์–ด result ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.
5+
6+
encode:
7+
Time: O(n) (n: strs ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ์—ฐ์‚ฐ)
8+
Space: O(1)
9+
10+
decode:
11+
Time: O(n) (n: s ์˜ ๊ธธ์ด๋งŒํผ ์—ฐ์‚ฐ)
12+
Space: O(m) (m: decode ์ดํ›„ ๋ฐฐ์—ด์˜ ๊ธธ์ด)
13+
"""
14+
15+
16+
class Codec:
17+
18+
def encode(self, strs: List[str]) -> str:
19+
"""Encodes a list of strings to a single string."""
20+
result = ""
21+
for word in strs:
22+
result += str(len(word))
23+
result += "#"
24+
result += word
25+
return result
26+
27+
def decode(self, s: str) -> List[str]:
28+
"""Decodes a single string to a list of strings."""
29+
i = 0
30+
result = []
31+
length = ""
32+
while i < len(s):
33+
# find number
34+
length = ""
35+
while s[i] is not "#":
36+
length += s[i]
37+
i += 1
38+
# find #
39+
i += 1
40+
# find word
41+
result.append(s[i : i + int(length)])
42+
i += int(length)
43+
44+
return result
45+
46+
47+
# Your Codec object will be instantiated and called as such:
48+
# codec = Codec()
49+
# codec.decode(codec.encode(strs))
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Solution: 1) DFS Brute Force -> TLE
3+
Time: O(2^n * nlogn)
4+
"""
5+
6+
7+
class Solution:
8+
def lengthOfLIS(self, nums: List[int]) -> int:
9+
10+
sub = []
11+
max_len = 0
12+
13+
def dfs(i, length):
14+
nonlocal max_len
15+
if i == len(nums):
16+
if sub == sorted(list(set(sub))):
17+
max_len = max(len(sub), max_len)
18+
return
19+
20+
dfs(i + 1, length)
21+
sub.append(nums[i])
22+
dfs(i + 1, length)
23+
sub.pop()
24+
25+
dfs(0, 0)
26+
return max_len
27+
28+
29+
"""
30+
ํ’€์ด๋ฅผ ๋ณด๊ณ  ์ ์—ˆ์œผ๋ฉฐ, ์™„๋ฒฝํžˆ ์ดํ•ด ๋˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค.
31+
Time: O(n^2)
32+
Space: O(n)
33+
"""
34+
35+
36+
class Solution:
37+
def lengthOfLIS(self, nums: List[int]) -> int:
38+
LIS = [1] * len(nums)
39+
40+
for i in range(len(nums) - 1, -1, -1):
41+
for j in range(i + 1, len(nums)):
42+
if nums[i] < nums[j]:
43+
LIS[i] = max(LIS[i], 1 + LIS[j])
44+
return max(LIS)

โ€Žspiral-matrix/Chaedie.pyโ€Ž

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Solution:
3+
1) ์ขŒ์šฐ ์ƒํ•˜์˜ ๊ฒฝ๊ณ„๋ฅผ ์ขํ˜€๊ฐ€๋ฉฐ ์ˆœํšŒํ•œ๋‹ค.
4+
2) ํƒˆ์ถœ ์กฐ๊ฑด์œผ๋กœ result ์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ matrix ์˜ ์›์†Œ ๊ฐฏ์ˆ˜๊ฐ€ ๊ฐ™์•„์ง€๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
5+
(ํƒˆ์ถœ ์กฐ๊ฑด์„ ์ขŒ์šฐ ์ƒํ•˜ ๊ฒฝ๊ณ„๊ฐ€ ๋„˜์–ด๊ฐ€๋Š” ์‹œํ—˜์„ ๊ธฐ์ค€์œผ๋กœ ์‚ผ์€ ์†”๋ฃจ์…˜์„ ๋ดค์ง€๋งŒ, ์ •ํ™•ํžˆ ์ดํ•ด๋Š” ๋˜์ง€ ์•Š์•„ ์‚ฌ์ด์ฆˆ ๋น„๊ต๋กœ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.)
6+
Time: O(m * n)
7+
Space: O(1)
8+
"""
9+
10+
11+
class Solution:
12+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
13+
result = []
14+
left, right = 0, len(matrix[0])
15+
top, bottom = 0, len(matrix)
16+
17+
while left < right and top < bottom:
18+
for i in range(left, right):
19+
result.append(matrix[top][i])
20+
top += 1
21+
22+
if len(result) == len(matrix[0]) * len(matrix):
23+
break
24+
25+
for i in range(top, bottom):
26+
result.append(matrix[i][right - 1])
27+
right -= 1
28+
29+
if len(result) == len(matrix[0]) * len(matrix):
30+
break
31+
32+
for i in range(right - 1, left - 1, -1):
33+
result.append(matrix[bottom - 1][i])
34+
bottom -= 1
35+
36+
if len(result) == len(matrix[0]) * len(matrix):
37+
break
38+
39+
for i in range(bottom - 1, top - 1, -1):
40+
result.append(matrix[i][left])
41+
left += 1
42+
43+
if len(result) == len(matrix[0]) * len(matrix):
44+
break
45+
46+
return result
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
Solution:
3+
1) s ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์—ฌ๋Š” ๊ด„ํ˜ธ๋ฉด stack ์— push ํ•œ๋‹ค.
4+
2) ๋‹ซ๋Š” ๊ด„ํ˜ธ์ผ ๊ฒฝ์šฐ ๊ฐ case ๋ณ„๋กœ stack ์˜ ์ตœ์ƒ๋‹จ์ด ์•Œ๋งž๋Š” ์—ฌ๋Š” ๊ด„ํ˜ธ์ด๋ฉด stack ์„ pop ํ•˜๊ณ  ์•„๋‹ˆ๋ฉด False ๋ฅผ return ํ•œ๋‹ค.
5+
Time: O(n)
6+
Space: O(n)
7+
"""
8+
9+
10+
class Solution:
11+
def isValid(self, s: str) -> bool:
12+
stack = []
13+
left = ["(", "{", "["]
14+
for char in s:
15+
if char in left:
16+
stack.append(char)
17+
continue
18+
19+
if not stack:
20+
return False
21+
22+
if char == ")":
23+
if stack[-1] == "(":
24+
stack.pop()
25+
else:
26+
return False
27+
if char == "}":
28+
if stack[-1] == "{":
29+
stack.pop()
30+
else:
31+
return False
32+
if char == "]":
33+
if stack[-1] == "[":
34+
stack.pop()
35+
else:
36+
return False
37+
38+
return not stack
39+
40+
41+
"""
42+
Solution:
43+
1) stack๊ณผ hash map์„ ์‚ฌ์šฉํ•œ๋‹ค.
44+
2) s๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์—ฌ๋Š” ๊ด„ํ˜ธ์ผ ๊ฒฝ์šฐ stack ์— push
45+
3) ๋‹ซ๋Š” ๊ด„ํ˜ธ์ผ ๊ฒฝ์šฐ stack ์ด ๋น„์ง€ ์•Š์œผ๋ฉด์„œ ์ตœ์ƒ๋‹จ ์š”์†Œ๊ฐ€ ์•Œ๋งž์€ ์—ฌ๋Š” ๊ด„ํ˜ธ์ด๋ฉด stack pop, ์•„๋‹ ๊ฒฝ์šฐ return False
46+
47+
Time: O(n)
48+
Space: O(n)
49+
"""
50+
51+
52+
class Solution:
53+
def isValid(self, s: str) -> bool:
54+
opening = "({["
55+
closing = ")}]"
56+
closeToOpen = dict(zip(closing, opening))
57+
58+
stack = []
59+
for char in s:
60+
if char in opening:
61+
stack.append(char)
62+
else:
63+
if stack and stack[-1] == closeToOpen[char]:
64+
stack.pop()
65+
else:
66+
return False
67+
return not stack

0 commit comments

Comments
ย (0)