File tree 1 file changed +7
-21
lines changed
scripts/algorithms/S/Search a 2D Matrix
1 file changed +7
-21
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime : 38 ms (Top 96.94 % ) | Memory : 17.90 MB (Top 11.07 % )
2
+
1
3
class Solution :
2
4
def searchMatrix (self , matrix : List [List [int ]], target : int ) -> bool :
3
- left = 0
4
- right = len (matrix )- 1
5
- while left <= right :
6
- mid = (left + right )// 2
7
- if matrix [mid ][0 ] < target :
8
- left = mid + 1
9
- elif matrix [mid ][0 ] > target :
10
- right = mid - 1
11
- else :
12
- return True
13
- left1 = 0
14
- right1 = len (matrix [right ])- 1
15
- while left1 <= right1 :
16
- mid = (left1 + right1 )// 2
17
- if matrix [right ][mid ] < target :
18
- left1 = mid + 1
19
- elif matrix [right ][mid ] > target :
20
- right1 = mid - 1
21
- else :
22
- return True
23
- return False
5
+ n = len (matrix [0 ])
6
+ def get (idx : int ) -> int :
7
+ r , c = divmod (idx , n )
8
+ return matrix [r ][c ]
9
+ return get (bisect_left (range (len (matrix )* n - 1 ), target , key = get )) == target
You can’t perform that action at this time.
0 commit comments