-
-
Notifications
You must be signed in to change notification settings - Fork 195
[ayosecu] Week 07 Solutions #1469
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,34 @@ | ||
class Solution: | ||
""" | ||
- Time Complexity: O(n), n = len(s) | ||
- Space Complexity: O(n) | ||
""" | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
check_set = set() | ||
|
||
longest_length, length = 0, 0 | ||
l, r = 0, 0 | ||
while r < len(s): | ||
# check each character (s[r]) is duplicated, and expand or narrow the length | ||
if s[r] not in check_set: | ||
check_set.add(s[r]) | ||
length += 1 | ||
longest_length = max(longest_length, length) | ||
r += 1 | ||
else: | ||
check_set.remove(s[l]) | ||
length -= 1 | ||
l += 1 | ||
|
||
return longest_length | ||
|
||
tc = [ | ||
("abcabcbb", 3), | ||
("bbbbb", 1), | ||
("pwwkew", 3) | ||
] | ||
|
||
sol = Solution() | ||
for i, (s, e) in enumerate(tc, 1): | ||
r = sol.lengthOfLongestSubstring(s) | ||
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}") | ||
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,47 @@ | ||
from typing import List | ||
|
||
class Solution: | ||
""" | ||
- Time Complexity: O(N), N = The number of items = len(grid) * len(grid[0]) | ||
- Space Complexity: O(N) | ||
- In worst case (all "1"s), The depth of dfs is N => O(N) | ||
""" | ||
def numIslands(self, grid: List[List[str]]) -> int: | ||
m, n = len(grid), len(grid[0]) | ||
|
||
def dfs(i, j): | ||
if i < 0 or i >= m or j < 0 or j >= n or grid[i][j] != "1": | ||
return | ||
|
||
grid[i][j] = "#" # Visited | ||
for dx, dy in [(1, 0), (0, 1), (0, -1), (-1, 0)]: | ||
dfs(i + dx, j + dy) | ||
|
||
count = 0 | ||
for i in range(m): | ||
for j in range(n): | ||
if grid[i][j] == "1": | ||
count += 1 | ||
dfs(i, j) | ||
|
||
return count | ||
|
||
tc = [ | ||
([ | ||
["1","1","1","1","0"], | ||
["1","1","0","1","0"], | ||
["1","1","0","0","0"], | ||
["0","0","0","0","0"] | ||
], 1), | ||
([ | ||
["1","1","0","0","0"], | ||
["1","1","0","0","0"], | ||
["0","0","1","0","0"], | ||
["0","0","0","1","1"] | ||
], 3) | ||
] | ||
|
||
sol = Solution() | ||
for i, (grid, e) in enumerate(tc, 1): | ||
r = sol.numIslands(grid) | ||
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}") |
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,50 @@ | ||
from typing import Optional | ||
|
||
# Definition for singly-linked list. | ||
class ListNode: | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
|
||
class Solution: | ||
""" | ||
- Time Complexity: O(n), n = The number of nodes. | ||
- Space Complexity: O(1) | ||
""" | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
pre = None | ||
while head: | ||
nxt = head.next | ||
head.next = pre | ||
pre = head | ||
head = nxt | ||
|
||
return pre | ||
|
||
def toLinkedList(lists): | ||
dummy = ListNode(-1) | ||
ptr = dummy | ||
for item in lists: | ||
ptr.next = ListNode(item) | ||
ptr = ptr.next | ||
return dummy.next | ||
|
||
def toList(head): | ||
result = [] | ||
|
||
while head: | ||
result.append(head.val) | ||
head = head.next | ||
|
||
return result | ||
|
||
tc = [ | ||
([1, 2, 3, 4, 5], [5, 4, 3, 2, 1]), | ||
([1, 2], [2, 1]), | ||
([], []) | ||
] | ||
|
||
sol = Solution() | ||
for i, (l, e) in enumerate(tc, 1): | ||
r = toList(sol.reverseList(toLinkedList(l))) | ||
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}") |
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,44 @@ | ||
from typing import List | ||
|
||
class Solution: | ||
""" | ||
- Time Complexity: O(mn), m = len(matrix), n = len(matrix[0]) | ||
- Space Complexity: O(1) | ||
""" | ||
def setZeroes(self, matrix: List[List[int]]) -> None: | ||
""" | ||
Do not return anything, modify matrix in-place instead. | ||
""" | ||
m = len(matrix) | ||
n = len(matrix[0]) | ||
|
||
# Update row and column with a flag (infinite) if the value is not zero | ||
def updateRowCol(i, j): | ||
for k in range(m): | ||
if matrix[k][j] != 0: | ||
matrix[k][j] = float("inf") | ||
for k in range(n): | ||
if matrix[i][k] != 0: | ||
matrix[i][k] = float("inf") | ||
|
||
# Visit all and update row and column if the value is zero | ||
for i in range(m): | ||
for j in range(n): | ||
if matrix[i][j] == 0: | ||
updateRowCol(i, j) | ||
|
||
# Update flagged data to zero | ||
for i in range(m): | ||
for j in range(n): | ||
if matrix[i][j] == float("inf"): | ||
matrix[i][j] = 0 | ||
|
||
tc = [ | ||
([[1,1,1],[1,0,1],[1,1,1]], [[1,0,1],[0,0,0],[1,0,1]]), | ||
([[0,1,2,0],[3,4,5,2],[1,3,1,5]], [[0,0,0,0],[0,4,5,0],[0,3,1,0]]) | ||
] | ||
|
||
sol = Solution() | ||
for i, (m, e) in enumerate(tc, 1): | ||
sol.setZeroes(m) | ||
print(f"TC {i} is Passed!" if m == e else f"TC {i} is Failed! - Expected: {e}, Result: {m}") |
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,24 @@ | ||
class Solution: | ||
""" | ||
- Time Complexity: O(mn) | ||
- Space Complexity: O(mn), the "dp" variable | ||
""" | ||
def uniquePaths(self, m: int, n: int) -> int: | ||
dp = [ [1] * n for _ in range(m) ] | ||
|
||
# DP Approach | ||
for i in range(1, m): | ||
for j in range(1, n): | ||
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] | ||
|
||
return dp[-1][-1] | ||
|
||
tc = [ | ||
(3, 7, 28), | ||
(3, 2, 3) | ||
] | ||
|
||
sol = Solution() | ||
for i, (m, n, e) in enumerate(tc, 1): | ||
r = sol.uniquePaths(m, n) | ||
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}") | ||
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. 이전에도 느꼈지만, @ayosecu 님 코드는 항상 간결하고 이해하기 쉬운 것 같습니다. DP 풀이가 참고가 되었습니다! |
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.
@ayosecu 님 안녕하세요!
저는 딕셔너리를 활용해서 풀었는데, 이 방식이 조금 더 직관적인 것처럼 느껴졌습니다. 좋은 풀이법 알려주셔서 감사합니다!