-
Notifications
You must be signed in to change notification settings - Fork 120
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
[kayden] Week 07 Solutions #487
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f4ad89
Reverse Linked List Solution
da4df4f
Longest Substring Without Repeating Characters Solution
9a4fe5e
Refactor Reverse Linked List
579e493
Number of Islands Solution
a647a2b
Unique Paths Solution
74bfb9f
Set Matrix Zeroes Solution
00458c7
Fix Set Matrix Zeroes
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,20 @@ | ||
class Solution: | ||
# 시간복잡도: O(N) | ||
# 공간복잡도: O(N) | ||
# set에서 제외하는 로직을 없애기 위해, map을 사용해서 idx값을 저장후, start와 비교해서 start보다 작은 idx를 가진 경우에는 중복이 아니라고 판단했습니다. | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
|
||
last_idx = {} | ||
answer = 0 | ||
start = 0 | ||
|
||
for idx, ch in enumerate(s): | ||
# 중복 조회시 idx값과 start 값 비교를 통해, 삭제하는 로직을 없이 중복을 확인했습니다. | ||
if ch in last_idx and last_idx[ch] >= start: | ||
start = last_idx[ch] + 1 | ||
last_idx[ch] = idx | ||
else: | ||
answer = max(answer, idx - start + 1) | ||
last_idx[ch] = idx | ||
|
||
return answer |
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,36 @@ | ||
# 시간복잡도: O(M*N) | ||
# 공간복잡도: O(M*N) | ||
|
||
from collections import deque | ||
|
||
|
||
class Solution: | ||
def numIslands(self, grid: List[List[str]]) -> int: | ||
dx = [0, 0, -1, 1] | ||
dy = [-1, 1, 0, 0] | ||
m = len(grid) | ||
n = len(grid[0]) | ||
q = deque() | ||
|
||
def bfs(a, b): | ||
q.append((a, b)) | ||
while q: | ||
x, y = q.popleft() | ||
|
||
for i in range(4): | ||
nx = x + dx[i] | ||
ny = y + dy[i] | ||
if not (0 <= nx < m and 0 <= ny < n): continue | ||
|
||
if grid[nx][ny] == '1': | ||
grid[nx][ny] = '0' | ||
q.append((nx, ny)) | ||
|
||
count = 0 | ||
for i in range(m): | ||
for j in range(n): | ||
if grid[i][j] == '1': | ||
count += 1 | ||
bfs(i, j) | ||
|
||
return count |
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 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
*/ | ||
// 시간복잡도: O(N) | ||
// 공간복잡도: O(1) | ||
class Solution { | ||
public ListNode reverseList(ListNode head) { | ||
ListNode prev = null; | ||
|
||
while (head != null){ | ||
ListNode next = head.next; | ||
head.next = prev; | ||
prev = head; | ||
head = next; | ||
} | ||
|
||
return prev; | ||
} | ||
} |
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(m*n) | ||
# 공간복잡도: O(m+n) | ||
class Solution: | ||
def setZeroes(self, matrix: List[List[int]]) -> None: | ||
m = len(matrix) | ||
n = len(matrix[0]) | ||
|
||
rows = set() | ||
cols = set() | ||
|
||
for i in range(m): | ||
for j in range(n): | ||
if matrix[i][j] == 0: | ||
rows.add(i) | ||
cols.add(j) | ||
|
||
for row in rows: | ||
for j in range(n): | ||
matrix[row][j] = 0 | ||
|
||
for col in cols: | ||
for i in range(m): | ||
matrix[i][col] = 0 |
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,17 @@ | ||
from math import comb | ||
class Solution: | ||
# 시간복잡도: O(m+n) | ||
# 공간복잡도: O(1) | ||
def uniquePaths(self, m: int, n: int) -> int: | ||
return comb(m+n-2, n-1) # m+n-2Cn-1 | ||
|
||
# 시간복잡도: O(m*n) | ||
# 공간복잡도: O(n) | ||
def uniquePaths2(self, m: int, n: int) -> int: | ||
dp = [1] * n | ||
|
||
for _ in range(1, m): | ||
for j in range(1, n): | ||
dp[j] = dp[j-1] + dp[j] | ||
Comment on lines
+11
to
+15
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. 저는 배열 두 개를 관리 했는데, 배열 하나면 충분하군요 잘 배워갑니다 :) 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. 오 그렇네요 좋은 풀이 감사합니다! |
||
|
||
return dp[n-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.
반복문 내에서 새로운 ListNode를 만들어내기 때문에 공간 복잡도 또한 N에 비례하여 선형적으로 증가할 것 같습니다
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.
답변 감사합니다! 최적화해서 수정 했습니다!