-
-
Notifications
You must be signed in to change notification settings - Fork 245
[devyejin] WEEK 09 solutions #1913
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
Open
devyejin
wants to merge
4
commits into
DaleStudy:main
Choose a base branch
from
devyejin:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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,35 @@ | ||
# Definition for singly-linked list. | ||
class ListNode: | ||
def __init__(self, x): | ||
self.val = x | ||
self.next = None | ||
|
||
from typing import Optional | ||
|
||
# time complexity O(n) | ||
# space complexity O(1) | ||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
slow, fast = head, head | ||
while fast and fast.next: | ||
slow = slow.next | ||
fast = fast.next.next | ||
|
||
if slow == fast: | ||
return True | ||
return False | ||
|
||
|
||
# time complexity O(n) | ||
# space complexity O(n) | ||
# class Solution: | ||
# def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
# visited = set() | ||
# while head: | ||
# if head in visited: | ||
# return True | ||
# visited.add(head) | ||
# head = head.next | ||
# return False | ||
|
||
|
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,19 @@ | ||
from typing import List | ||
|
||
# time O(n), space O(1) | ||
class Solution: | ||
def maxProduct(self, nums: List[int]) -> int: | ||
max_prod = nums[0] | ||
min_prod = nums[0] | ||
result = nums[0] | ||
|
||
for i in range(1, len(nums)): | ||
candidates = (nums[i], nums[i] * max_prod, nums[i] * min_prod) | ||
max_prod, min_prod = max(candidates), min(candidates) | ||
result = max(result, max_prod) | ||
|
||
return result | ||
|
||
|
||
|
||
|
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,45 @@ | ||
from typing import List | ||
from collections import deque | ||
|
||
# time O(mn), saoce O(mn) | ||
class Solution: | ||
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: | ||
m, n = len(heights), len(heights[0]) | ||
# 방문 배열 | ||
pacific = [[False] * n for _ in range(m)] | ||
atlantic = [[False] * n for _ in range(m)] | ||
|
||
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] | ||
|
||
def bfs(r, c, visited): | ||
queue = deque([(r, c)]) | ||
visited[r][c] = True | ||
|
||
while queue: | ||
r, c = queue.popleft() | ||
|
||
for dr, dc in directions: | ||
new_r, new_c = r + dr, c + dc | ||
if (0 <= new_r < m and 0 <= new_c < n | ||
and not visited[new_r][new_c] | ||
and heights[new_r][new_c] >= heights[r][c]): | ||
visited[new_r][new_c] = True | ||
queue.append((new_r, new_c)) | ||
|
||
for i in range(n): | ||
bfs(0, i, pacific) | ||
bfs(m - 1, i, atlantic) | ||
for i in range(m): | ||
bfs(i, 0, pacific) | ||
bfs(i, n - 1, atlantic) | ||
|
||
result = [] | ||
for i in range(m): | ||
for j in range(n): | ||
if pacific[i][j] and atlantic[i][j]: | ||
result.append([i, j]) | ||
|
||
return result | ||
|
||
|
||
|
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.
파이썬 while 문법 형태가 조금 달라서 신기하네요!
간결하고 읽기 쉬운 코드입니다