Skip to content

[Chaedie] Week 9 #996

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 5 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 40 additions & 0 deletions find-minimum-in-rotated-sorted-array/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Solution:
1) 순회하며 이전 값이 현재 값보다 크거나 같다면 현재 값이 최소값이다.
2) 끝까지 돌아도 최소값이 없을 경우 첫번쨰 값이 최소값이다.
Time: O(n)
Copy link
Contributor

Choose a reason for hiding this comment

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

문제 조건에 이런게 있습니다 😔

You must write an algorithm that runs in O(log n) time.

이 문제의 난이도가 medium인 이유..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

헛... 해당 조건을 놓쳤네요 감사합니다.. 이게 medium이라고? 하면서 풀었네요.. 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

binary search 풀이도 추가했습니다..!

Space: O(1)
"""


class Solution:
def findMin(self, nums: List[int]) -> int:
for i in range(1, len(nums)):
if nums[i - 1] >= nums[i]:
return nums[i]

return nums[0]

"""
Solution:
시간 복잡도 O(log n)으로 풀기 위해 binary search 사용
Time: O(log n)
Space: O(1)
"""

def findMin(self, nums: List[int]) -> int:
l, r = 1, len(nums) - 1

while l <= r:
mid = (l + r) // 2
# prev 값보다 mid 가 작으면 찾던 값
if nums[mid - 1] > nums[mid]:
return nums[mid]
# mid 까지 정상 순서면 우측 탐색
if nums[0] < nums[mid]:
l = mid + 1
# mid 까지 비 정상 순서면 좌측 탐색
else:
r = mid - 1
# 못찾을 경우 전체 정상 순서 케이스
return nums[0]
24 changes: 24 additions & 0 deletions linked-list-cycle/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Solution:
1) 사이클이 있다면 무한 루프가 발생할것이다.
2) 사이클이 없다면 언젠가 null 이 될것이다.
3) 따라서 두개의 포인터를 사용하여 동일한 Node에 도달하는 지 확인한다.
4) null 이 된다면 사이클이 없다는 뜻이다.
Time: O(n)
Space: O(1)
"""


class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
if not head or not head.next:
return False

slow = head
fast = head.next
while slow and fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
37 changes: 37 additions & 0 deletions maximum-product-subarray/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution:
"""
Brute Force

Time: O(n^2)
Space: O(1)
"""

def maxProduct(self, nums: List[int]) -> int:

max_prod = float(-inf)
for i in range(len(nums)):
prod = nums[i]
max_prod = max(max_prod, prod)
for j in range(i + 1, len(nums)):
prod *= nums[j]
max_prod = max(max_prod, prod)

return max_prod

"""
최소곱, 최대곱을 모두 저장하면서 최대값을 찾는다.
(음수 곱 양수곱을 모두 커버하기 위해 최소곱도 저장한다.)

Time: O(n)
Space: O(1)
"""

def maxProduct(self, nums: List[int]) -> int:
result = nums[0]
min_prod, max_prod = 1, 1
for num in nums:
arr = [min_prod * num, max_prod * num, num]
min_prod = min(arr)
max_prod = max(arr)
result = max(max_prod, result)
return result
33 changes: 33 additions & 0 deletions pacific-atlantic-water-flow/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Solution:
1) 가장자리에서 시작해서 어디까지 올라갈수있는지 체크한다.
2) 교집합을 찾는다.

Time: O(m * n)
Space: O(m * n)
"""


class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
ROWS, COLS = len(heights), len(heights[0])
pacific, atlantic = set(), set()

def dfs(visit, r, c):
if (r, c) in visit:
return
visit.add((r, c))

for i, j in [(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)]:
if 0 <= i and i < ROWS and 0 <= j and j < COLS:
if heights[i][j] >= heights[r][c]:
dfs(visit, i, j)

for i in range(ROWS):
dfs(pacific, i, 0)
dfs(atlantic, i, COLS - 1)
for i in range(COLS):
dfs(pacific, 0, i)
dfs(atlantic, ROWS - 1, i)

return list(pacific.intersection(atlantic))