-
-
Notifications
You must be signed in to change notification settings - Fork 195
[나리] WEEK 07 Solutions #489
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
7 commits
Select commit
Hold shift + click to select a range
f651ab1
Reverse Linked List Solution
naringst 6c88f0e
Longest Substring Without Repeating Characters
naringst 4e22d71
fix: Add end line break
naringst 64467df
fix: Add line break again
naringst 8818b5f
Number Of Islands Solution
naringst c47ebed
fix: add line break
naringst cefbb6b
Update Time Complexity
naringst 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
22 changes: 22 additions & 0 deletions
22
longest-substring-without-repeating-characters/naringst.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,22 @@ | ||
|
||
# Runtime: 51ms, Memory: 16.83MB | ||
# Time complexity: O(len(s)^2) | ||
# Space complexity: O(len(s)) | ||
|
||
class Solution: | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
stringArr = [] | ||
maxLength = 0 | ||
|
||
for sub in s : | ||
if sub in stringArr : | ||
maxLength = max(maxLength, len(stringArr)) | ||
repeatIdx = stringArr.index(sub) | ||
stringArr = stringArr[repeatIdx+1 :] | ||
|
||
stringArr.append(sub) | ||
|
||
maxLength = max(maxLength, len(stringArr)) | ||
|
||
|
||
return maxLength |
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 collections import deque | ||
|
||
# Runtime: 241ms, Memory: 18.94MB | ||
# Time complexity: O(len(n*m)) | ||
# Space complexity: O(len(n*m)) | ||
|
||
|
||
class Solution: | ||
def bfs(self, a,b, grid, visited) : | ||
n = len(grid) | ||
m = len(grid[0]) | ||
|
||
dx = [0, 0, 1, -1] | ||
dy = [1, -1 ,0 ,0] | ||
q = deque() | ||
q.append([a,b]) | ||
visited[a][b] = True | ||
|
||
while q : | ||
x,y = q.popleft() | ||
|
||
for i in range(4) : | ||
nx = x + dx[i] | ||
ny = y + dy[i] | ||
|
||
if (0 <= nx < n and 0 <= ny < m and not visited[nx][ny] and grid[nx][ny] == '1'): | ||
visited[nx][ny] = True | ||
q.append([nx,ny]) | ||
|
||
|
||
|
||
def numIslands(self, grid: List[List[str]]) -> int: | ||
n = len(grid) | ||
m = len(grid[0]) | ||
visited = [[False] * m for _ in range(n)] | ||
answer = 0 | ||
|
||
for i in range(n) : | ||
for j in range(m) : | ||
if grid[i][j] == '1' and not visited[i][j] : | ||
self.bfs(i,j,grid,visited) | ||
answer += 1 | ||
|
||
return answer |
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,27 @@ | ||
|
||
# Runtime: 39ms, Memory: 17.88MB | ||
# Time complexity: O(len(head)) | ||
# Space complexity: O(len(head)) | ||
DaleSeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
|
||
class Solution: | ||
def __init__(self): | ||
self.nodes = [] # ListNode 객체를 저장할 배열 | ||
|
||
def reverseList(self, head: Optional[ListNode]) -> List[Optional[ListNode]]: | ||
prev = None | ||
curr = head | ||
|
||
while curr is not None: | ||
nextNode = curr.next | ||
curr.next = prev | ||
prev = curr | ||
curr = nextNode | ||
|
||
return prev |
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.
Uh oh!
There was an error while loading. Please reload this page.