Skip to content

Commit 50a6e9e

Browse files
authored
Merge pull request #911 from pmjuu/main
[Lyla] Week 06
2 parents 16e6492 + 9d995c6 commit 50a6e9e

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
3+
- ๋‘ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™ํ•˜๋ฉด์„œ ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ž…๋‹ˆ๋‹ค.
4+
5+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
6+
- ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(1)์ž…๋‹ˆ๋‹ค.
7+
'''
8+
9+
from typing import List
10+
11+
12+
class Solution:
13+
def maxArea(self, height: List[int]) -> int:
14+
max_area = 0
15+
left, right = 0, len(height) - 1
16+
17+
while left < right:
18+
current_area = (right - left) * min(height[left], height[right])
19+
max_area = max(current_area, max_area)
20+
21+
if height[left] < height[right]:
22+
left += 1
23+
else:
24+
right -= 1
25+
26+
return max_area
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'''
2+
* L: ๋‹จ์–ด์˜ ๊ธธ์ด
3+
์‹œ๊ฐ„๋ณต์žก๋„: O(1)
4+
- addWord(word): O(L), ์ตœ๋Œ€ ๋‹จ์–ด ๊ธธ์ด๊ฐ€ 25๋กœ ์ œํ•œ๋˜๋ฏ€๋กœ ์ด ์ž‘์—…์€ ์ƒ์ˆ˜ ์‹œ๊ฐ„์— ๊ฐ€๊น์Šต๋‹ˆ๋‹ค.
5+
- search(word): O(L * 26^d), ์—ฌ๊ธฐ์„œ 26์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. d๋Š” ๋‹จ์–ด ๋‚ด '.'์˜ ๊ฐœ์ˆ˜์ธ๋ฐ, 2๋กœ ์ œํ•œ๋˜๋ฏ€๋กœ ์ƒ์ˆ˜ ์‹œ๊ฐ„์— ๊ฐ€๊น์Šต๋‹ˆ๋‹ค.
6+
๊ณต๊ฐ„๋ณต์žก๋„:
7+
- Trie ๊ตฌ์กฐ์— ์ €์žฅ๋˜๋Š” ๋ฌธ์ž์˜ ์ˆ˜์— ๋น„๋ก€ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด์˜ ์ด ๊ธธ์ด๋ฅผ T๋ผ๊ณ  ํ•˜๋ฉด ๊ณต๊ฐ„๋ณต์žก๋„๋Š” O(T) ์ž…๋‹ˆ๋‹ค.
8+
'''
9+
10+
11+
class Trie:
12+
def __init__(self):
13+
self.children = {}
14+
self.is_end_of_word = False
15+
16+
class WordDictionary:
17+
18+
def __init__(self):
19+
# Trie์˜ ๋ฃจํŠธ ๋…ธ๋“œ ์ดˆ๊ธฐํ™”
20+
self.root = Trie()
21+
22+
def addWord(self, word: str) -> None:
23+
current = self.root
24+
25+
for char in word:
26+
if char not in current.children:
27+
current.children[char] = Trie()
28+
29+
current = current.children[char]
30+
31+
current.is_end_of_word = True
32+
33+
def search(self, word: str) -> bool:
34+
def dfs(node, i):
35+
if i == len(word):
36+
return node.is_end_of_word
37+
38+
char = word[i]
39+
if char == '.':
40+
# '.'์ธ ๊ฒฝ์šฐ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ์— ๋Œ€ํ•ด ํƒ์ƒ‰
41+
for child in node.children.values():
42+
if dfs(child, i + 1):
43+
return True
44+
return False
45+
46+
if char not in node.children:
47+
return False
48+
49+
return dfs(node.children[char], i + 1)
50+
51+
return dfs(self.root, 0)
52+
53+
54+
55+
# Your WordDictionary object will be instantiated and called as such:
56+
# obj = WordDictionary()
57+
# obj.addWord(word)
58+
# param_2 = obj.search(word)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
Dynamic programming ํ™œ์šฉ
3+
4+
์‹œ๊ฐ„๋ณต์žก๋„: O(n^2) - ๋‘ ๊ฐœ์˜ ์ค‘์ฒฉ๋œ ๋ฐ˜๋ณต๋ฌธ์ด nums ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•จ
5+
๊ณต๊ฐ„๋ณต์žก๋„: O(n) - dp ๋ฐฐ์—ด์— ์ˆซ์ž ๊ฐœ์ˆ˜(n)๋งŒํผ ๊ณต๊ฐ„์ด ํ•„์š”ํ•จ
6+
'''
7+
8+
def lengthOfLIS_n2(nums):
9+
n = len(nums)
10+
dp = [1] * n
11+
12+
for i in range(1, n):
13+
for j in range(i):
14+
if nums[j] < nums[i]:
15+
dp[i] = max(dp[i], dp[j] + 1) # ์ด์ „ LIS ๊ธธ์ด์— 1 ์ถ”๊ฐ€
16+
17+
return max(dp) # dp ๋ฐฐ์—ด์˜ ์ตœ๋Œ€๊ฐ’์ด ์ตœ์žฅ ๊ธธ์ด
18+
19+
20+
'''
21+
์ด์ง„ํƒ์ƒ‰ ํ™œ์šฉ
22+
23+
์‹œ๊ฐ„๋ณต์žก๋„: O(n log n) - ๊ฐ ์ˆซ์ž์— ๋Œ€ํ•ด ์ด์ง„ ํƒ์ƒ‰(bisect_left)์„ ์ˆ˜ํ–‰ํ•จ
24+
๊ณต๊ฐ„๋ณต์žก๋„: O(n) - sub ๋ฆฌ์ŠคํŠธ์— ์ตœ๋Œ€ n๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ์ €์žฅ๋  ์ˆ˜ ์žˆ์Œ
25+
'''
26+
27+
from bisect import bisect_left
28+
29+
def lengthOfLIS_nlogn(nums):
30+
sub = [] # ํ˜„์žฌ๊นŒ์ง€ ์ฐพ์€ LIS์˜ ์ˆซ์ž๋“ค์„ ์ €์žฅ
31+
32+
for num in nums:
33+
pos = bisect_left(sub, num) # ์‚ฝ์ž… ์œ„์น˜๋ฅผ ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ์ฐพ์Œ
34+
35+
if pos == len(sub):
36+
sub.append(num) # ์‚ฝ์ž… ์œ„์น˜๊ฐ€ sub์˜ ๋ฒ”์œ„ ๋ฐ–์ด๋ฉด ์ˆซ์ž ์ถ”๊ฐ€
37+
else:
38+
sub[pos] = num # ์‚ฝ์ž… ์œ„์น˜๊ฐ€ ๋ฒ”์œ„ ์•ˆ์ด๋ฉด ํ•ด๋‹น ์œ„์น˜์˜ ์ˆซ์ž๋ฅผ ํ˜„์žฌ ์ˆซ์ž๋กœ ๊ต์ฒด
39+
40+
return len(sub)

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
์‹œ๊ฐ„๋ณต์žก๋„: O(m * n) - ๋ชจ๋“  ํ–‰๋ ฌ์˜ ์š”์†Œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธ
3+
๊ณต๊ฐ„๋ณต์žก๋„: O(1) - ์ถ”๊ฐ€ ๊ณต๊ฐ„ ์—†์ด ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅ
4+
'''
5+
from typing import List
6+
7+
8+
class Solution:
9+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
10+
result = []
11+
top, bottom = 0, len(matrix) - 1
12+
left, right = 0, len(matrix[0]) - 1
13+
14+
while top <= bottom and left <= right:
15+
for col in range(left, right + 1):
16+
result.append(matrix[top][col])
17+
top += 1
18+
19+
for row in range(top, bottom + 1):
20+
result.append(matrix[row][right])
21+
right -= 1
22+
23+
if top <= bottom:
24+
for col in range(right, left - 1, -1):
25+
result.append(matrix[bottom][col])
26+
bottom -= 1
27+
28+
if left <= right:
29+
for row in range(bottom, top - 1, -1):
30+
result.append(matrix[row][left])
31+
left += 1
32+
33+
return result

โ€Žvalid-parentheses/pmjuu.pyโ€Ž

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

0 commit comments

Comments
ย (0)