-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Lyla] Week 06 #911
Open
pmjuu
wants to merge
2
commits into
DaleStudy:main
Choose a base branch
from
pmjuu:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−0
Open
[Lyla] Week 06 #911
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 변수명이 직관적이여서 가독성이 좋네요 :) |
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
두 가지 방법에 대한 풀이과 설명이 너무 좋습니다!