-
-
Notifications
You must be signed in to change notification settings - Fork 195
[sejineer] Week 06 solutions #1415
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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 hidden or 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,17 @@ | ||
""" | ||
시간 복잡도: O(N) | ||
공간 복잡도: O(1) | ||
""" | ||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
result = 0 | ||
start, end = 0, len(height) - 1 | ||
|
||
while start < end: | ||
area = (end - start) * min(height[start], height[end]) | ||
result = max(result, area) | ||
if height[start] < height[end]: | ||
start += 1 | ||
else: | ||
end -= 1 | ||
return result |
This file contains hidden or 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 @@ | ||
class WordDictionary: | ||
|
||
def __init__(self): | ||
self.root = {"$": True} | ||
|
||
|
||
def addWord(self, word: str) -> None: | ||
node = self.root | ||
for ch in word: | ||
if ch not in node: | ||
node[ch] = {"$": False} | ||
node = node[ch] | ||
node["$"] = True | ||
|
||
def search(self, word: str) -> bool: | ||
def dfs(node, idx): | ||
if idx == len(word): | ||
return node["$"] | ||
ch = word[idx] | ||
if ch in node: | ||
return dfs(node[ch], idx + 1) | ||
elif ch == ".": | ||
return any(dfs(node[k], idx + 1) for k in node if k != "$") | ||
else: | ||
return False | ||
return dfs(self.root, 0) |
This file contains hidden or 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,16 @@ | ||
""" | ||
시간 복잡도: O(Nlog(N)) | ||
공간 복잡도: O(N) | ||
""" | ||
from bisect import bisect_left | ||
|
||
class Solution: | ||
def lengthOfLIS(self, nums: List[int]) -> int: | ||
sub = [] | ||
for num in nums: | ||
index = bisect_left(sub, num) | ||
if index == len(sub): | ||
sub.append(num) | ||
else: | ||
sub[index] = num | ||
return len(sub) |
This file contains hidden or 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,29 @@ | ||
""" | ||
시간 복잡도: O(n * m) | ||
공간 복잡도: O(n * m) | ||
""" | ||
class Solution: | ||
def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
n, m = len(matrix), len(matrix[0]) | ||
dx = [1, 0, -1, 0] | ||
dy = [0, 1, 0, -1] | ||
state = 0 | ||
vis = [[False] * m for _ in range(n)] | ||
|
||
result = [matrix[0][0]] | ||
vis[0][0] = True | ||
x, y = 0, 0 | ||
|
||
while len(result) < n * m: | ||
nx = x + dx[state % 4] | ||
ny = y + dy[state % 4] | ||
|
||
if not (0 <= nx < m) or not (0 <= ny < n) or vis[ny][nx]: | ||
state += 1 | ||
continue | ||
|
||
vis[ny][nx] = True | ||
result.append(matrix[ny][nx]) | ||
x, y = nx, ny | ||
|
||
return result | ||
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. vis 배열 없는 풀이도 참고하시면 좋을 것 같아요! class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if not matrix or not matrix[0]:
return []
result = []
top, bottom = 0, len(matrix) - 1
left, right = 0, len(matrix[0]) - 1
while left <= right and top <= bottom:
result += matrix[top][left:right + 1]
top += 1
result += [matrix[i][right] for i in range(top, bottom + 1)]
right -= 1
if top <= bottom:
result += matrix[bottom][right:left - 1:-1] if right >= left else []
bottom -= 1
if left <= right:
result += [matrix[i][left] for i in range(bottom, top - 1, -1)]
left += 1
return result 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 hidden or 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,21 @@ | ||
""" | ||
시간 복잡도: O(N) | ||
공간 복잡도: O(N) | ||
""" | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
pair = {'(': ')', '{': '}', '[': ']'} | ||
stack = [] | ||
for c in s: | ||
if c in ('(', '{', '['): | ||
stack.append(c) | ||
else: | ||
if stack: | ||
cur = stack.pop() | ||
if pair[cur] == c: | ||
continue | ||
else: | ||
return False | ||
else: | ||
return False | ||
return not stack |
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.
any() 로 pythonic 하게 처리한 점 좋아요!