-
-
Notifications
You must be signed in to change notification settings - Fork 195
[i-mprovising] Week 07 solutions #1459
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
6 commits
Select commit
Hold shift + click to select a range
330eded
week 7 reverse-linked-list
i-mprovising b78caa7
longest-substring-without-repeating-characters
i-mprovising 3113880
number-of-islands
i-mprovising 1ff9936
fix linelint
i-mprovising e19e8c1
add unique-paths
i-mprovising 5a40ebe
add set-matrix-zeroes
i-mprovising 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
37 changes: 37 additions & 0 deletions
37
longest-substring-without-repeating-characters/i-mprovising.py
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,37 @@ | ||
from collections import deque | ||
|
||
class Solution: | ||
""" | ||
Time complexity O(n) | ||
Space complexity O(n) | ||
""" | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
max_len = 0 | ||
q = deque() | ||
for i, ch in enumerate(s): | ||
if ch not in q: | ||
q.append(ch) | ||
if len(q) > max_len: | ||
max_len = len(q) | ||
else: | ||
while True: | ||
tmp = q.popleft() | ||
if tmp == ch: | ||
break | ||
q.append(ch) | ||
|
||
return max_len | ||
|
||
def slidingWindow(self, s): | ||
start, end = 0, 0 | ||
substr = set() | ||
max_len = 0 | ||
while end < len(s): | ||
if s[end] in substr: | ||
substr.remove(s[start]) | ||
start += 1 | ||
else: | ||
substr.add(s[end]) | ||
end += 1 | ||
max_len = max(end - start, max_len) | ||
return max_len |
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,32 @@ | ||
class Solution: | ||
""" | ||
Time, Space comlexity O(n*m) | ||
|
||
connected components | ||
dfs, bfs | ||
""" | ||
def numIslands(self, grid: List[List[str]]) -> int: | ||
n, m = len(grid), len(grid[0]) | ||
visited = [[False for _ in range(m)] for _ in range(n)] # visited 대신 grid를 0으로 표시할수도 있다 | ||
islands = 0 | ||
|
||
def dfs(x, y): | ||
stack = [(x, y)] | ||
while stack: | ||
x, y = stack.pop() | ||
dx = [-1, 1, 0, 0] | ||
dy = [0, 0, -1, 1] | ||
for k in range(4): | ||
nx, ny = x + dx[k], y + dy[k] | ||
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.
|
||
if 0<=nx<=n-1 and 0<=ny<=m-1: | ||
if not visited[nx][ny] and grid[nx][ny] == "1": | ||
visited[nx][ny] = True | ||
stack.append((nx, ny)) | ||
|
||
for i in range(n): | ||
for j in range(m): | ||
if not visited[i][j] and grid[i][j] == "1": | ||
dfs(i, j) | ||
islands += 1 | ||
|
||
return islands |
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,25 @@ | ||
# 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) | ||
Space complexity O(1) | ||
""" | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
if not head or not head.next: | ||
return head | ||
|
||
node = head.next | ||
prev = head | ||
prev.next = None | ||
|
||
while node: | ||
next_node = node.next | ||
node.next = prev | ||
prev = node | ||
node = next_node | ||
|
||
return prev |
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,23 @@ | ||
""" | ||
Time, space complexity O(m * n) | ||
""" | ||
|
||
class Solution: | ||
def setZeroes(self, matrix: List[List[int]]) -> None: | ||
""" | ||
Do not return anything, modify matrix in-place instead. | ||
""" | ||
m, n = len(matrix), len(matrix[0]) | ||
i_indices = set() | ||
j_indices = set() | ||
for i in range(m): | ||
for j in range(n): | ||
if matrix[i][j] == 0: | ||
i_indices.add(i) | ||
j_indices.add(j) | ||
|
||
for i in i_indices: | ||
matrix[i] = [0 for _ in range(n)] | ||
for j in j_indices: | ||
for i in range(m): | ||
matrix[i][j] = 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,21 @@ | ||
""" | ||
Time, space complexity O(m*n) | ||
|
||
Dynamic programming | ||
""" | ||
|
||
class Solution: | ||
def uniquePaths(self, m: int, n: int) -> int: | ||
dp = [[0 for _ in range(n)] for _ in range(m)] | ||
dp[0] = [1 for _ in range(n)] | ||
|
||
for i in range(1, m): | ||
for j in range(n): | ||
if i == 0: | ||
continue | ||
if j == 0: | ||
dp[i][j] = 1 | ||
continue | ||
dp[i][j] = dp[i-1][j] + dp[i][j-1] | ||
|
||
return dp[-1][-1] |
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.
deque
를 이용하는 방식 흥미롭게 잘 보았습니다!