Skip to content
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

python #2180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

python #2180

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
25 changes: 25 additions & 0 deletions problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'''
// Time Complexity : O(logmn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english : same implementation as in class

// Your code here along with comments explaining your approach
'''
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
m = len(matrix)
n = len(matrix[0])
if m == 0: return False

l, r = 0, m * n - 1
while l <= r:
mid = (l + r) // 2
num = matrix[mid // n][mid % n]
if target == num:
return True
if target < num:
r = mid - 1
else:
l = mid + 1
return False
30 changes: 30 additions & 0 deletions problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'''
// Time Complexity : O(logn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english : same implementation as in class

// Your code here along with comments explaining your approach
'''
class Solution:
def search(self, nums: List[int], target: int) -> int:
l, r = 0, len(nums) - 1

while l <= r:
mid = (l + r) // 2
if target == nums[mid]:
return mid

# left sorted portion
if nums[l] <= nums[mid]:
if target > nums[mid] or target < nums[l]:
l = mid + 1
else:
r = mid - 1
# right sorted portion
else:
if target < nums[mid] or target > nums[r]:
r = mid - 1
else:
l = mid + 1
return -1
34 changes: 34 additions & 0 deletions problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'''
// Time Complexity : O(logn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english : same implementation as in class

// Your code here along with comments explaining your approach
'''
# """
# This is ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
#class ArrayReader:
# def get(self, index: int) -> int:

class Solution:
def search(self, reader: 'ArrayReader', target: int) -> int:
l = 0
r = 1

while reader.get(r) < target:
l = r
r *= 2

while l <= r:
mid = l + (r - l) // 2
if reader.get(mid) == target: return mid

if reader.get(mid) > target:
r = mid - 1
else:
l += 1

return -1