forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request DaleStudy#1004 from aa601/main
[yeoju] Week 9
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,17 @@ | ||
''' | ||
TC: O(log n) | ||
SC: O(1) | ||
''' | ||
|
||
class Solution: | ||
def findMin(self, nums: List[int]) -> int: | ||
left = 0 | ||
right = len(nums) - 1 | ||
mid = (left + right) // 2 | ||
while left < right: | ||
mid = (left + right) // 2 | ||
if nums[mid] > nums[right]: | ||
left = mid + 1 | ||
else: | ||
right = mid | ||
return nums[left] |
This file contains 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,14 @@ | ||
''' | ||
TC : O(n) | ||
SC : O(n) | ||
''' | ||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
tmp = head | ||
visited = set() | ||
while tmp: | ||
if tmp in visited: | ||
return True | ||
visited.add(tmp) | ||
tmp = tmp.next | ||
return False |
This file contains 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,14 @@ | ||
''' | ||
TC : O(n) | ||
SC : 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)): | ||
max_prod, min_prod = max(nums[i], nums[i] * max_prod, nums[i] * min_prod), min(nums[i], nums[i] * min_prod, nums[i] * max_prod) | ||
result = max(result, max_prod) | ||
return result |
This file contains 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,28 @@ | ||
''' | ||
TC: O(r * c) | ||
SC: O(r * c) | ||
''' | ||
|
||
class Solution: | ||
def pacificAtlantic(self, heights: list[list[int]]) -> list[list[int]]: | ||
r_size = len(heights) | ||
c_size = len(heights[0]) | ||
pcf = set() | ||
atl = set() | ||
|
||
def recur(visited, row, col) -> None : | ||
if (row, col) in visited: | ||
return | ||
visited.add((row, col)) | ||
for r, c in ([row + 1, col], [row - 1, col], [row, col + 1], [row, col - 1]): | ||
if 0 <= r < r_size and 0 <= c < c_size \ | ||
and heights[r][c] >= heights[row][col]: | ||
recur(visited, r, c) | ||
for r in range(r_size): | ||
recur(pcf, r, 0) | ||
recur(atl, r, c_size - 1) | ||
for c in range(c_size): | ||
recur(pcf, 0, c) | ||
recur(atl, r_size - 1, c) | ||
result = [[r, c] for r, c in pcf & atl] | ||
return result |