|
| 1 | +--- |
| 2 | +id: search-a-2D-matrix |
| 3 | +title: Search a 2D matrix |
| 4 | +difficulty: Medium |
| 5 | +sidebar_label: 0074-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 \times 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 | +``` |
| 36 | +Matrix = [[1,3,5,7], [10,11,16,20], [23,30,34,60]] |
| 37 | +Target = 3 |
| 38 | +``` |
| 39 | + |
| 40 | +**Output**: true |
| 41 | + |
| 42 | +#### Example 2: |
| 43 | +**Input**: |
| 44 | +``` |
| 45 | +Matrix = [[1,3,5,7], [10,11,16,20], [23,30,34,60]] |
| 46 | +Target = 13 |
| 47 | +``` |
| 48 | + |
| 49 | +**Output**: false |
| 50 | + |
| 51 | + |
| 52 | +### Constraints |
| 53 | + |
| 54 | +- $m == \text{matrix.length}$ |
| 55 | +- $n == \text{matrix}[i].\text{length}$ |
| 56 | +- $1 \leq m, n \leq 100$ |
| 57 | +- $-10^{4} \leq \text{matrix}[i][j], \text{target} \leq 10^{4}$ |
| 58 | + |
| 59 | +### Approach |
| 60 | + |
| 61 | +The steps are as follows: |
| 62 | + |
| 63 | +- 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. |
| 64 | + |
| 65 | +- Calculate the ‘mid’: Now, inside the loop, we will calculate the value of ‘mid’ using the following formula: |
| 66 | +mid = (low+high) // 2 ( ‘//’ refers to integer division) |
| 67 | + |
| 68 | +- 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. |
| 69 | +row = mid / M, col = mid % M. |
| 70 | + - If matrix[row][col] == target: We should return true here, as we have found the ‘target’. |
| 71 | + |
| 72 | + - 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). |
| 73 | + - 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). |
| 74 | + |
| 75 | +- Steps 2-3 will be inside a while loop and the loop will end once low crosses high |
| 76 | +(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. |
| 77 | + |
| 78 | +### Solution Code |
| 79 | + |
| 80 | +#### Python |
| 81 | + |
| 82 | +``` |
| 83 | +class Solution: |
| 84 | + def searchMatrix(matrix, target): |
| 85 | + n = len(matrix) |
| 86 | + m = len(matrix[0]) |
| 87 | +
|
| 88 | + # apply binary search: |
| 89 | + low = 0 |
| 90 | + high = n * m - 1 |
| 91 | + while low <= high: |
| 92 | + mid = (low + high) // 2 |
| 93 | + row = mid // m |
| 94 | + col = mid % m |
| 95 | + if matrix[row][col] == target: |
| 96 | + return True |
| 97 | + elif matrix[row][col] < target: |
| 98 | + low = mid + 1 |
| 99 | + else: |
| 100 | + high = mid - 1 |
| 101 | + return False |
| 102 | +
|
| 103 | +``` |
| 104 | + |
| 105 | +#### Java |
| 106 | + |
| 107 | +``` |
| 108 | +class Solution { |
| 109 | + public static boolean searchMatrix(ArrayList<ArrayList<Integer>> matrix, int target) { |
| 110 | + int n = matrix.size(); |
| 111 | + int m = matrix.get(0).size(); |
| 112 | +
|
| 113 | + //apply binary search: |
| 114 | + int low = 0, high = n * m - 1; |
| 115 | + while (low <= high) { |
| 116 | + int mid = (low + high) / 2; |
| 117 | + int row = mid / m, col = mid % m; |
| 118 | + if (matrix.get(row).get(col) == target) return true; |
| 119 | + else if (matrix.get(row).get(col) < target) low = mid + 1; |
| 120 | + else high = mid - 1; |
| 121 | + } |
| 122 | + return false; |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +#### C++ |
| 128 | + |
| 129 | +``` |
| 130 | +class Solution { |
| 131 | + public: |
| 132 | + bool searchMatrix(vector<vector<int>>& matrix, int target) { |
| 133 | + int n = matrix.size(); |
| 134 | + int m = matrix[0].size(); |
| 135 | +
|
| 136 | + //apply binary search: |
| 137 | + int low = 0, high = n * m - 1; |
| 138 | + while (low <= high) { |
| 139 | + int mid = (low + high) / 2; |
| 140 | + int row = mid / m, col = mid % m; |
| 141 | + if (matrix[row][col] == target) return true; |
| 142 | + else if (matrix[row][col] < target) low = mid + 1; |
| 143 | + else high = mid - 1; |
| 144 | + } |
| 145 | + return false; |
| 146 | +} |
| 147 | +}; |
| 148 | +
|
| 149 | +``` |
| 150 | + |
| 151 | +### Conclusion |
| 152 | + |
| 153 | +Complexity Analysis |
| 154 | + |
| 155 | +- Time Complexity: $O(log(N.M))$, where N = given row number, M = given column number. |
| 156 | + |
| 157 | + Reason: We are applying binary search on the imaginary 1D array of size NxM. |
| 158 | + |
| 159 | +- Space Complexity: $O(1)$ as we are not using any extra space. |
0 commit comments