Skip to content

[SunaDu] Week 9 #991

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 8 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions find-minimum-in-rotated-sorted-array/dusunax.py
Copy link
Contributor

@TonyKim9401 TonyKim9401 Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

파이썬의 min 함수를 사용하여 간단한 풀이를 보여주셨네요!
해당 문제의 키포인트는 어떻게 하면 O(log n) 의 시간 복잡도로 최솟값을 찾을 수 있을까를 물어보는 문제입니다.
다시 한번 풀어보시면 좋을것 같아요!

Copy link
Member Author

@dusunax dusunax Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

키포인트를 놓쳤는데 submit이 통과되어서 혼자 풀었다면 왜 medium이지?하고 놓쳤을 것 같아요
리뷰 감사합니다!!🙌

# 153. Find Minimum in Rotated Sorted Array
> **why binary search works in a "rotated" sorted array?**
> rotated sorted array consists of **two sorted subarrays**, and the minimum value is the second sorted subarray's first element.
> so 👉 find the point that second sorted subarray starts.
>
> - if nums[mid] > nums[right]? => the pivot point is in the right half.
> - if nums[mid] <= nums[right]? => the pivot point is in the left half.
> - loop until left and right are the same.

def findMinBS(self, nums: List[int]) -> int:
left = 0
right = len(nums) - 1
while left < right:
mid = (left + right) // 2
if nums[mid] > nums[right]:
left = mid + 1
else:
right = mid
return nums[left]

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'''
# 153. Find Minimum in Rotated Sorted Array

## Time Complexity: O(n)
- min() iterates through all elements to find the smallest one.

## Space Complexity: O(1)
- no extra space is used.
'''
class Solution:
def findMin(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]

return min(nums)
33 changes: 33 additions & 0 deletions linked-list-cycle/dusunax.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LinkedList의 특성을 잘 살려서 풀이해주신것 같습니다 :)

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'''
# 141. Linked List Cycle

use two pointers, Floyd's Tortoise and Hare algorithm

> Tortoise and Hare algorithm
>- slow pointer moves one step at a time
>- fast pointer moves two steps at a time
>- if there is a cycle, slow and fast will meet at some point
>- if there is no cycle, fast will reach the end of the list

## Time Complexity: O(n)
In the worst case, we need to traverse the entire list to determine if there is a cycle.

## Space Complexity: O(1)
no extra space is used, only the two pointers.
'''
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
if not head or not head.next:
return False

slow = head
fast = head

while fast and fast.next:
slow = slow.next
fast = fast.next.next

if slow == fast:
return True

return False
42 changes: 42 additions & 0 deletions pacific-atlantic-water-flow/dusunax.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이번주 2번째로 어려웠던 문제라고 생각되는데요.
dfs 알고리즘을 사용하여 너무나 깔끔하게 풀이해 주신 것 같습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'''
# 417. Pacific Atlantic Water Flow

## Time Complexity: O(n * m)
- dfs is called for each cell in the grid, and each cell is visited once.

## Space Complexity: O(n * m)
- pacific and atlantic sets store the cells that can flow to the pacific and atlantic oceans respectively.
'''
class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
if len(heights) == 1 and len(heights[0]) == 1:
return [[0, 0]]

max_row, max_col = len(heights), len(heights[0])
pacific, atlantic = set(), set()
directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def dfs(r, c, visited, prev_height):
out_of_bound = r < 0 or c < 0 or r >= max_row or c >= max_col
if out_of_bound:
return

current = heights[r][c]
is_visited = (r, c) in visited
is_uphill = current < prev_height
if is_visited or is_uphill:
return

visited.add((r, c))

for dr, dc in directions:
dfs(r + dr, c + dc, visited, current)

for r in range(max_row):
dfs(r, 0, pacific, heights[r][0]) # left
dfs(r, max_col - 1, atlantic, heights[r][max_col - 1]) # right
for c in range(max_col):
dfs(0, c, pacific, heights[0][c]) # top
dfs(max_row - 1, c, atlantic, heights[max_row - 1][c]) # bottom

return list(pacific & atlantic)