-
-
Notifications
You must be signed in to change notification settings - Fork 335
[ohkingtaek] WEEK7 Solutions #2548
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
4 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
23 changes: 23 additions & 0 deletions
23
longest-substring-without-repeating-characters/ohkingtaek.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,23 @@ | ||
| """ | ||
| Time Complexity: O(n^2) | ||
| Space Complexity: O(n) | ||
|
|
||
| 과정: | ||
| 1. 문자열을 순회하면서 중복되지 않는 가장 긴 부분 문자열을 찾음 | ||
| 2. 중복되지 않는 부분 문자열을 찾으면 그 길이를 최대 길이와 비교하여 최대 길이를 업데이트함 | ||
| 3. 중복되는 부분 문자열을 찾으면 그 부분 문자열을 초기화함 | ||
| 4. 중복되지 않는 부분 문자열을 찾으면 그 부분 문자열을 최대 길이와 비교하여 최대 길이를 업데이트함 | ||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| ans = 0 | ||
| for left in range(len(s)): | ||
| tmp = set() | ||
| for right in range(left, len(s)): | ||
| if s[right] in tmp: | ||
| break | ||
| tmp.add(s[right]) | ||
| ans = max(right - left + 1, ans) | ||
| return ans |
|
Contributor
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,32 @@ | ||
| """ | ||
| Time Complexity: O(m * n) | ||
| Space Complexity: O(m * n) | ||
|
|
||
| 과정: | ||
| 1. 2차원 배열을 순회하면서 1을 만나면 섬의 개수를 증가시킴 | ||
| 2. DFS를 통해 해당 섬을 탐색함 | ||
| 3. 해당 섬을 탐색하면서 0을 만나면 탐색을 종료함 | ||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
| def numIslands(self, grid: List[List[str]]) -> int: | ||
| ans = 0 | ||
| m, n = len(grid[0]), len(grid) | ||
| visited = [[0] * m for _ in range(n)] | ||
| dx, dy = [-1, 0, 1, 0], [0, -1, 0, 1] | ||
| for y in range(n): | ||
| for x in range(m): | ||
| if int(grid[y][x]) == 1 and visited[y][x] == 0: | ||
| ans += 1 | ||
| dfs = [(x, y)] | ||
| visited[y][x] = 1 | ||
| while dfs: | ||
| cx, cy = dfs.pop(0) | ||
| for _dx, _dy in zip(dx, dy): | ||
| gy = cy + _dy | ||
| gx = cx + _dx | ||
| if gx >= 0 and gy >= 0 and gx < m and gy < n and int(grid[gy][gx]) == 1 and visited[gy][gx] == 0: | ||
| visited[gy][gx] = 1 | ||
| dfs.append((gx, gy)) | ||
| return ans |
|
Contributor
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,25 @@ | ||
| """ | ||
| Time Complexity: O(n) | ||
| Space Complexity: O(1) | ||
|
|
||
| 과정: | ||
| 1. 두 개의 포인터를 사용하여 리스트를 순회하면서 뒤집음 | ||
| 2. 첫 번째 포인터는 이전 노드를 가리키고, 두 번째 포인터는 현재 노드를 가리킴 | ||
| 3. 현재 노드의 next를 이전 노드로 변경하고, 두 포인터를 한 칸씩 이동시킴 | ||
| 4. 마지막 노드까지 반복하면 리스트가 뒤집어짐 | ||
| """ | ||
|
|
||
|
|
||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| left, now = None, head | ||
| while now: | ||
| right = now.next | ||
| now.next = left | ||
| left, now = now, right | ||
| return left |
|
Contributor
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,31 @@ | ||
| """ | ||
| Time Complexity: O(m * n) | ||
| Space Complexity: O(m + n) | ||
|
|
||
| 과정: | ||
| 1. 0이 있는 행과 열을 따로 집합에 기록해둠 | ||
| 2. 만들어진 행과 열을 기반으로 0으로 바꿔줌 | ||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
| def setZeroes(self, matrix: List[List[int]]) -> None: | ||
| """ | ||
| Do not return anything, modify matrix in-place instead. | ||
| """ | ||
| m, n = len(matrix[0]), len(matrix) | ||
| array = [] | ||
| for x in range(m): | ||
| for y in range(n): | ||
| if matrix[y][x] == 0: | ||
| array.append((x, y)) | ||
| a, b = set(), set() | ||
| for x, y in array: | ||
| a.add(x) | ||
| b.add(y) | ||
| for x in range(m): | ||
| for i in b: | ||
| matrix[i][x] = 0 | ||
| for y in range(n): | ||
| for i in a: | ||
| matrix[y][i] = 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.
🏷️ 알고리즘 패턴 분석