Skip to content

Commit 4b2e5ad

Browse files
Search2DMatrix.java
1 parent b1fe8bc commit 4b2e5ad

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed

src/main/java/com/thealgorithms/matrix/Search2DMatrix.java

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,82 @@
22

33
/**
44
* Two variations of searching in a 2D matrix
5-
*
5+
*
66
* Variation 1:
77
* - Each row is sorted left to right
88
* - Each column is sorted top to bottom
9-
*
9+
*
1010
* Variation 2:
1111
* - The matrix is a flattened sorted array
1212
* - i.e., row 0 ends, row 1 continues (matrix is sorted as a 1D array)
13-
*
13+
*
1414
* LeetCode: #74 and #240
1515
*/
1616
public class Search2DMatrix {
1717

1818
/**
19-
* Variation 1: Search in a matrix where each row and each column is sorted
20-
* Time: O(m + n)
19+
* Search in a 2D matrix where rows and columns are sorted.
2120
*
22-
* @param matrix input matrix
23-
* @param target value to find
24-
* @return true if found, false otherwise
21+
* @param matrix The 2D matrix with sorted rows and columns.
22+
* @param target The target integer to search for.
23+
* @return true if target is found, otherwise false.
2524
*/
2625
public static boolean searchMatrixSortedRowsAndCols(int[][] matrix, int target) {
27-
int rows = matrix.length;
28-
if (rows == 0) return false;
26+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
27+
return false;
28+
}
2929

30+
int rows = matrix.length;
3031
int cols = matrix[0].length;
31-
int row = 0, col = cols - 1;
32+
33+
int row = 0;
34+
int col = cols - 1;
3235

3336
while (row < rows && col >= 0) {
3437
int val = matrix[row][col];
35-
if (val == target) return true;
36-
else if (val > target) col--;
37-
else row++;
38+
if (val == target) {
39+
return true;
40+
} else if (val > target) {
41+
col--;
42+
} else {
43+
row++;
44+
}
3845
}
3946

4047
return false;
4148
}
4249

4350
/**
44-
* Variation 2: Search in a matrix that behaves like a flattened sorted array
45-
* Time: O(log(m * n))
51+
* Search in a 2D matrix that is sorted as if flattened into a 1D array.
4652
*
47-
* @param matrix input matrix
48-
* @param target value to find
49-
* @return true if found, false otherwise
53+
* @param matrix The 2D matrix.
54+
* @param target The target integer to search for.
55+
* @return true if target is found, otherwise false.
5056
*/
5157
public static boolean searchMatrixFlattenedSorted(int[][] matrix, int target) {
52-
int rows = matrix.length;
53-
if (rows == 0) return false;
58+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
59+
return false;
60+
}
5461

62+
int rows = matrix.length;
5563
int cols = matrix[0].length;
56-
int left = 0, right = rows * cols - 1;
64+
65+
int left = 0;
66+
int right = rows * cols - 1;
5767

5868
while (left <= right) {
5969
int mid = left + (right - left) / 2;
6070
int val = matrix[mid / cols][mid % cols];
6171

62-
if (val == target) return true;
63-
else if (val < target) left = mid + 1;
64-
else right = mid - 1;
72+
if (val == target) {
73+
return true;
74+
} else if (val < target) {
75+
left = mid + 1;
76+
} else {
77+
right = mid - 1;
78+
}
6579
}
6680

6781
return false;
6882
}
69-
70-
// Example usage
71-
public static void main(String[] args) {
72-
int[][] matrix1 = {
73-
{1, 4, 7, 11},
74-
{2, 5, 8, 12},
75-
{3, 6, 9, 16},
76-
{10, 13, 14, 17}
77-
};
78-
System.out.println(searchMatrixSortedRowsAndCols(matrix1, 5)); // true
79-
80-
int[][] matrix2 = {
81-
{1, 3, 5, 7},
82-
{10, 11, 16, 20},
83-
{23, 30, 34, 50}
84-
};
85-
System.out.println(searchMatrixFlattenedSorted(matrix2, 3)); // true
86-
}
8783
}

0 commit comments

Comments
 (0)