diff --git a/Sample b/Sample deleted file mode 100644 index ceae9f98..00000000 --- a/Sample +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach in three sentences only diff --git a/Sample.py b/Sample.py new file mode 100644 index 00000000..ab53ae9a --- /dev/null +++ b/Sample.py @@ -0,0 +1,28 @@ +# // Time Complexity : O(log(m*n)) +# // Space Complexity : O(1) +# // Did this code successfully run on Leetcode : Yes +# // Any problem you faced while coding this : No + + +# // Your code here along with comments explaining your approach in three sentences only +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + m = len(matrix) + n = len(matrix[0]) + low = 0 + high = (m*n) - 1 + while low<=high: + mid = low + (high-low)//2 + + #convert index to position on matrix + r = mid//n + c = mid%n + + if matrix[r][c] == target: + return True + elif matrix[r][c] > target: + high = mid - 1 + else: + low = mid + 1 + + return False \ No newline at end of file