|
| 1 | +--- |
| 2 | +id: search-a-2D-matrix |
| 3 | +title: Search a 2D matrix |
| 4 | +difficulty: Medium |
| 5 | +sidebar_label: 0073-search2Dmatrix |
| 6 | +tags: |
| 7 | + - Array |
| 8 | + - Binary Search |
| 9 | + - Matrix |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem |
| 13 | + |
| 14 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 15 | +| :---------------- | :------------ | :--------------- | |
| 16 | +| [Search a 2D matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Search a 2D matrix Solution on LeetCode](https://leetcode.com/problems/search-a-2d-matrix/solutions/) | [Leetcode Profile](https://leetcode.com/u/debangi_29/) | |
| 17 | + |
| 18 | +## Problem Description |
| 19 | + |
| 20 | +You are given an m x n integer matrix matrix with the following two properties: |
| 21 | + |
| 22 | +- Each row is sorted in non-decreasing order. |
| 23 | +- The first integer of each row is greater than the last integer of the previous row. |
| 24 | + |
| 25 | +Given an integer target, return true if target is in matrix or false otherwise. |
| 26 | + |
| 27 | +You must write a solution in $O(log(m * n))$ time complexity. |
| 28 | + |
| 29 | + |
| 30 | +### Examples |
| 31 | + |
| 32 | +#### Example 1: |
| 33 | + |
| 34 | +**Input**: |
| 35 | +Matrix = [[1,3,5,7], [10,11,16,20], [23,30,34,60]], target = 3 |
| 36 | + |
| 37 | +**Output**: true |
| 38 | + |
| 39 | +#### Example 2: |
| 40 | +**Input**: |
| 41 | +Matrix = [[1,3,5,7], [10,11,16,20], [23,30,34,60]], target = 13 |
| 42 | + |
| 43 | +**Output**: false |
| 44 | + |
| 45 | + |
| 46 | +### Constraints |
| 47 | + |
| 48 | +- m == matrix.length |
| 49 | +- n == matrix[i].length |
| 50 | +- 1 <= m, n <= 100 |
| 51 | +- -10^4 <= matrix[i][j], target <= 10^4 |
| 52 | + |
| 53 | +### Approach |
| 54 | + |
| 55 | +The steps are as follows: |
| 56 | + |
| 57 | +- Place the 2 pointers i.e. low and high: Initially, we will place the pointers. The pointer low will point to 0 and the high will point to (NxM)-1. |
| 58 | + |
| 59 | +- Calculate the ‘mid’: Now, inside the loop, we will calculate the value of ‘mid’ using the following formula: |
| 60 | +mid = (low+high) // 2 ( ‘//’ refers to integer division) |
| 61 | + |
| 62 | +- Eliminate the halves based on the element at index mid: To get the element, we will convert index ‘mid’ to the corresponding cell using the above formula. Here no. of columns of the matrix = M. |
| 63 | +row = mid / M, col = mid % M. |
| 64 | + - If matrix[row][col] == target: We should return true here, as we have found the ‘target’. |
| 65 | + |
| 66 | + - If matrix[row][col] < target: In this case, we need bigger elements. So, we will eliminate the left half and consider the right half (low = mid+1). |
| 67 | + - If matrix[row][col] > target: In this case, we need smaller elements. So, we will eliminate the right half and consider the left half (high = mid-1). |
| 68 | + |
| 69 | +- Steps 2-3 will be inside a while loop and the loop will end once low crosses high |
| 70 | +(i.e. low > high). If we are out of the loop, we can say the target does not exist in the matrix. So, we will return false. |
| 71 | + |
| 72 | +### Solution Code |
| 73 | + |
| 74 | +#### Python |
| 75 | + |
| 76 | +``` |
| 77 | +class Solution: |
| 78 | + def searchMatrix(matrix, target): |
| 79 | + n = len(matrix) |
| 80 | + m = len(matrix[0]) |
| 81 | +
|
| 82 | + # apply binary search: |
| 83 | + low = 0 |
| 84 | + high = n * m - 1 |
| 85 | + while low <= high: |
| 86 | + mid = (low + high) // 2 |
| 87 | + row = mid // m |
| 88 | + col = mid % m |
| 89 | + if matrix[row][col] == target: |
| 90 | + return True |
| 91 | + elif matrix[row][col] < target: |
| 92 | + low = mid + 1 |
| 93 | + else: |
| 94 | + high = mid - 1 |
| 95 | + return False |
| 96 | +
|
| 97 | +``` |
| 98 | + |
| 99 | +#### Java |
| 100 | + |
| 101 | +``` |
| 102 | +class Solution { |
| 103 | + public static boolean searchMatrix(ArrayList<ArrayList<Integer>> matrix, int target) { |
| 104 | + int n = matrix.size(); |
| 105 | + int m = matrix.get(0).size(); |
| 106 | +
|
| 107 | + //apply binary search: |
| 108 | + int low = 0, high = n * m - 1; |
| 109 | + while (low <= high) { |
| 110 | + int mid = (low + high) / 2; |
| 111 | + int row = mid / m, col = mid % m; |
| 112 | + if (matrix.get(row).get(col) == target) return true; |
| 113 | + else if (matrix.get(row).get(col) < target) low = mid + 1; |
| 114 | + else high = mid - 1; |
| 115 | + } |
| 116 | + return false; |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +#### C++ |
| 122 | + |
| 123 | +``` |
| 124 | +class Solution { |
| 125 | + public: |
| 126 | + bool searchMatrix(vector<vector<int>>& matrix, int target) { |
| 127 | + int n = matrix.size(); |
| 128 | + int m = matrix[0].size(); |
| 129 | +
|
| 130 | + //apply binary search: |
| 131 | + int low = 0, high = n * m - 1; |
| 132 | + while (low <= high) { |
| 133 | + int mid = (low + high) / 2; |
| 134 | + int row = mid / m, col = mid % m; |
| 135 | + if (matrix[row][col] == target) return true; |
| 136 | + else if (matrix[row][col] < target) low = mid + 1; |
| 137 | + else high = mid - 1; |
| 138 | + } |
| 139 | + return false; |
| 140 | +} |
| 141 | +}; |
| 142 | +
|
| 143 | +``` |
| 144 | + |
| 145 | +### Conclusion |
| 146 | + |
| 147 | +Complexity Analysis |
| 148 | + |
| 149 | +- Time Complexity: $O(log(NxM))$, where N = given row number, M = given column number. |
| 150 | + |
| 151 | + Reason: We are applying binary search on the imaginary 1D array of size NxM. |
| 152 | + |
| 153 | +- Space Complexity: $O(1)$ as we are not using any extra space. |
0 commit comments