-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode Problem 74 Search a 2D Matrix.txt
More file actions
110 lines (87 loc) · 3.16 KB
/
Leetcode Problem 74 Search a 2D Matrix.txt
File metadata and controls
110 lines (87 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
74. Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
Example 1:
Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 3
Output: true
Example 2:
Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 13
Output: false
Hint to solve: 2 methods
1st method: Use wrapping by division and modulo
2nd method: Use two binary search on row and cols
class Solution:
#treat 2d matrix as a sorted list and generate mapping
# row = mid/total_cols
# col = mid%total_cols
def method_1(self, matrix:List[List[int]], target:int) -> bool:
if len(matrix) == 0 or len(matrix[0]) == 0:
return False
total_rows = len(matrix)
total_cols = len(matrix[0])
start = 0
end = (total_rows * total_cols) - 1
while(start <= end):
mid = (int)((end+start)/2)
mid_value = matrix[(int)(mid/total_cols)][mid%total_cols]
#print(F"start:{start} end:{end} mid:{mid} midVal:{mid_value}")
if mid_value == target:
return True
if mid_value < target:
start = mid + 1
elif mid_value > target:
end = mid - 1
return False
#Perform 2 binary search
#First binary search on the row
#Second binary search on the col
def method_2(self, matrix, target) -> bool:
if len(matrix) == 0 or len(matrix[0]) == 0:
return False
total_rows = len(matrix)
total_cols = len(matrix[0])
#Binary search on row. To decide which row might contain the number
start = 0
end = total_rows - 1
rowIndex = 0
while(start <= end):
mid = (int)((start+end)/2)
rowIndex = mid
#print(F"start:{start} end:{end} mid:{mid} midValue:{matrix[mid][0]}")
if(matrix[mid][0] == target):
return True
if(matrix[mid][0] < target and matrix[mid][total_cols-1] >= target):
break
if matrix[mid][0] < target:
start = mid + 1
elif matrix[mid][0] > target:
end = mid - 1
#Binary search on column
start = 0
end = total_cols - 1
while(start <= end):
mid = (int)((start+end)/2)
#print(F"start:{start} end:{end} mid:{mid} midValue:{matrix[rowIndex][mid]}")
if(matrix[rowIndex][mid] == target):
return True
if matrix[rowIndex][mid] < target:
start = mid + 1
elif matrix[rowIndex][mid] > target:
end = mid - 1
return False
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
# return self.method_1(matrix, target)
return self.method_2(matrix, target)