-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #911 from pmjuu/main
[Lyla] Week 06
- Loading branch information
Showing
5 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
''' | ||
์๊ฐ ๋ณต์ก๋: O(n) | ||
- ๋ ํฌ์ธํฐ๋ฅผ ์ด๋ํ๋ฉด์ ๋ฐฐ์ด์ ํ ๋ฒ๋ง ์ํํ๋ฏ๋ก ์๊ฐ ๋ณต์ก๋๋ O(n)์ ๋๋ค. | ||
๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
- ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋ณ์๋ง ์ฌ์ฉํ๋ฏ๋ก O(1)์ ๋๋ค. | ||
''' | ||
|
||
from typing import List | ||
|
||
|
||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
max_area = 0 | ||
left, right = 0, len(height) - 1 | ||
|
||
while left < right: | ||
current_area = (right - left) * min(height[left], height[right]) | ||
max_area = max(current_area, max_area) | ||
|
||
if height[left] < height[right]: | ||
left += 1 | ||
else: | ||
right -= 1 | ||
|
||
return max_area |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
''' | ||
* L: ๋จ์ด์ ๊ธธ์ด | ||
์๊ฐ๋ณต์ก๋: O(1) | ||
- addWord(word): O(L), ์ต๋ ๋จ์ด ๊ธธ์ด๊ฐ 25๋ก ์ ํ๋๋ฏ๋ก ์ด ์์ ์ ์์ ์๊ฐ์ ๊ฐ๊น์ต๋๋ค. | ||
- search(word): O(L * 26^d), ์ฌ๊ธฐ์ 26์ ์ํ๋ฒณ ์๋ฌธ์์ ๊ฐ์๋ฅผ ์๋ฏธํฉ๋๋ค. d๋ ๋จ์ด ๋ด '.'์ ๊ฐ์์ธ๋ฐ, 2๋ก ์ ํ๋๋ฏ๋ก ์์ ์๊ฐ์ ๊ฐ๊น์ต๋๋ค. | ||
๊ณต๊ฐ๋ณต์ก๋: | ||
- Trie ๊ตฌ์กฐ์ ์ ์ฅ๋๋ ๋ฌธ์์ ์์ ๋น๋กํฉ๋๋ค. ๋จ์ด์ ์ด ๊ธธ์ด๋ฅผ T๋ผ๊ณ ํ๋ฉด ๊ณต๊ฐ๋ณต์ก๋๋ O(T) ์ ๋๋ค. | ||
''' | ||
|
||
|
||
class Trie: | ||
def __init__(self): | ||
self.children = {} | ||
self.is_end_of_word = False | ||
|
||
class WordDictionary: | ||
|
||
def __init__(self): | ||
# Trie์ ๋ฃจํธ ๋ ธ๋ ์ด๊ธฐํ | ||
self.root = Trie() | ||
|
||
def addWord(self, word: str) -> None: | ||
current = self.root | ||
|
||
for char in word: | ||
if char not in current.children: | ||
current.children[char] = Trie() | ||
|
||
current = current.children[char] | ||
|
||
current.is_end_of_word = True | ||
|
||
def search(self, word: str) -> bool: | ||
def dfs(node, i): | ||
if i == len(word): | ||
return node.is_end_of_word | ||
|
||
char = word[i] | ||
if char == '.': | ||
# '.'์ธ ๊ฒฝ์ฐ ๋ชจ๋ ์์ ๋ ธ๋์ ๋ํด ํ์ | ||
for child in node.children.values(): | ||
if dfs(child, i + 1): | ||
return True | ||
return False | ||
|
||
if char not in node.children: | ||
return False | ||
|
||
return dfs(node.children[char], i + 1) | ||
|
||
return dfs(self.root, 0) | ||
|
||
|
||
|
||
# Your WordDictionary object will be instantiated and called as such: | ||
# obj = WordDictionary() | ||
# obj.addWord(word) | ||
# param_2 = obj.search(word) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
''' | ||
Dynamic programming ํ์ฉ | ||
์๊ฐ๋ณต์ก๋: O(n^2) - ๋ ๊ฐ์ ์ค์ฒฉ๋ ๋ฐ๋ณต๋ฌธ์ด nums ๋ฐฐ์ด์ ํ์ํจ | ||
๊ณต๊ฐ๋ณต์ก๋: O(n) - dp ๋ฐฐ์ด์ ์ซ์ ๊ฐ์(n)๋งํผ ๊ณต๊ฐ์ด ํ์ํจ | ||
''' | ||
|
||
def lengthOfLIS_n2(nums): | ||
n = len(nums) | ||
dp = [1] * n | ||
|
||
for i in range(1, n): | ||
for j in range(i): | ||
if nums[j] < nums[i]: | ||
dp[i] = max(dp[i], dp[j] + 1) # ์ด์ LIS ๊ธธ์ด์ 1 ์ถ๊ฐ | ||
|
||
return max(dp) # dp ๋ฐฐ์ด์ ์ต๋๊ฐ์ด ์ต์ฅ ๊ธธ์ด | ||
|
||
|
||
''' | ||
์ด์งํ์ ํ์ฉ | ||
์๊ฐ๋ณต์ก๋: O(n log n) - ๊ฐ ์ซ์์ ๋ํด ์ด์ง ํ์(bisect_left)์ ์ํํจ | ||
๊ณต๊ฐ๋ณต์ก๋: O(n) - sub ๋ฆฌ์คํธ์ ์ต๋ n๊ฐ์ ์ซ์๊ฐ ์ ์ฅ๋ ์ ์์ | ||
''' | ||
|
||
from bisect import bisect_left | ||
|
||
def lengthOfLIS_nlogn(nums): | ||
sub = [] # ํ์ฌ๊น์ง ์ฐพ์ LIS์ ์ซ์๋ค์ ์ ์ฅ | ||
|
||
for num in nums: | ||
pos = bisect_left(sub, num) # ์ฝ์ ์์น๋ฅผ ์ด์ง ํ์์ผ๋ก ์ฐพ์ | ||
|
||
if pos == len(sub): | ||
sub.append(num) # ์ฝ์ ์์น๊ฐ sub์ ๋ฒ์ ๋ฐ์ด๋ฉด ์ซ์ ์ถ๊ฐ | ||
else: | ||
sub[pos] = num # ์ฝ์ ์์น๊ฐ ๋ฒ์ ์์ด๋ฉด ํด๋น ์์น์ ์ซ์๋ฅผ ํ์ฌ ์ซ์๋ก ๊ต์ฒด | ||
|
||
return len(sub) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
''' | ||
์๊ฐ๋ณต์ก๋: O(m * n) - ๋ชจ๋ ํ๋ ฌ์ ์์๋ฅผ ํ ๋ฒ์ฉ ๋ฐฉ๋ฌธ | ||
๊ณต๊ฐ๋ณต์ก๋: O(1) - ์ถ๊ฐ ๊ณต๊ฐ ์์ด ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅ | ||
''' | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
result = [] | ||
top, bottom = 0, len(matrix) - 1 | ||
left, right = 0, len(matrix[0]) - 1 | ||
|
||
while top <= bottom and left <= right: | ||
for col in range(left, right + 1): | ||
result.append(matrix[top][col]) | ||
top += 1 | ||
|
||
for row in range(top, bottom + 1): | ||
result.append(matrix[row][right]) | ||
right -= 1 | ||
|
||
if top <= bottom: | ||
for col in range(right, left - 1, -1): | ||
result.append(matrix[bottom][col]) | ||
bottom -= 1 | ||
|
||
if left <= right: | ||
for row in range(bottom, top - 1, -1): | ||
result.append(matrix[row][left]) | ||
left += 1 | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
''' | ||
์๊ฐ ๋ณต์ก๋: O(n) | ||
- ๋ฌธ์์ด s์ ๊ธธ์ด๋ฅผ n์ด๋ผ๊ณ ํ ๋, ๋ฌธ์์ด์ ๊ฐ ๋ฌธ์๋ฅผ ํ ๋ฒ์ฉ ์ํํ๋ฉฐ ์ฒ๋ฆฌํ๋ฏ๋ก O(n)์ ๋๋ค. | ||
๊ณต๊ฐ ๋ณต์ก๋: O(n) | ||
- ์คํ์ ์ด๋ฆฐ ๊ดํธ๋ฅผ ์ ์ฅํ๋ ๋ฐ ์ฌ์ฉ๋๋ ๊ณต๊ฐ์ด ์ต์ ์ ๊ฒฝ์ฐ ๋ฌธ์์ด s์ ๊ธธ์ด n๊ณผ ๊ฐ์ ์ ์์ผ๋ฏ๋ก O(n)์ ๋๋ค. | ||
''' | ||
|
||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
stack = [] | ||
bracket_map = {")": "(", "}": "{", "]": "["} | ||
|
||
for bracket in s: | ||
if bracket in bracket_map: | ||
if stack and stack[-1] == bracket_map[bracket]: | ||
stack.pop() | ||
else: | ||
return False | ||
else: | ||
stack.append(bracket) | ||
|
||
return len(stack) == 0 |