Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ LeetCode
|77|[Combinations](https://leetcode.com/problems/combinations/)| [C++](./algorithms/cpp/combinations/combinations.cpp)|Medium|
|76|[Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)| [C++](./algorithms/cpp/minimumWindowSubstring/minimumWindowSubstring.cpp)|Hard|
|75|[Sort Colors](https://leetcode.com/problems/sort-colors/)| [C++](./algorithms/cpp/sortColors/sortColors.cpp)|Medium|
|74|[Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/)| [C++](./algorithms/cpp/search2DMatrix/search2DMatrix.cpp), [Java](./algorithms/java/src/search2DMatrix/search2DMatrix.java)|Medium|
|74|[Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/)| [C++](./algorithms/cpp/search2DMatrix/search2DMatrix.cpp), [Java](./algorithms/java/src/search2DMatrix/search2DMatrix.java), [TypeScript](algorithms/ts/search2DMatrix/search-2d-matrix.ts)|Medium|
|73|[Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/)| [C++](./algorithms/cpp/setMatrixZeroes/setMatrixZeroes.cpp)|Medium|
|72|[Edit Distance](https://leetcode.com/problems/edit-distance/)| [C++](./algorithms/cpp/editDistance/editDistance.cpp)|Hard|
|71|[Simplify Path](https://leetcode.com/problems/simplify-path/)| [C++](./algorithms/cpp/simplifyPath/simplifyPath.cpp)|Medium|
Expand Down
75 changes: 75 additions & 0 deletions algorithms/ts/search2DMatrix/search-2d-matrix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Source : https://oj.leetcode.com/problems/search-a-2d-matrix/
// Author : Hao Chen
// Date : 2014-06-23

/**********************************************************************************
*
* 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.
*
* For example,
*
* Consider the following matrix:
*
* [
* [1, 3, 5, 7],
* [10, 11, 16, 20],
* [23, 30, 34, 50]
* ]
*
* Given target = 3, return true.
*
**********************************************************************************/
function searchMatrix(matrix: number[][], target: number): boolean {
// Define pointers for the top and bottom row of the matrix
let top = 0;
let bottom = matrix.length - 1;

// First, perform binary search over the rows
while (top <= bottom) {
// Pick the middle row
const row = Math.floor((top + bottom) / 2);

// If target is greater than the last element in this row,
// move search window down (to rows below)
if (target > matrix[row][matrix[row].length - 1]) {
top = row + 1;
}
// If target is smaller than the first element in this row,
// move search window up (to rows above)
else if (target < matrix[row][0]) {
bottom = row - 1;
}
// Otherwise, the target must be within this row
else {
// Perform binary search within the row
let l = 0;
let r = matrix[row].length - 1;

while (l <= r) {
const mid = Math.floor((l + r) / 2);

// If element matches target, return true
if (matrix[row][mid] === target) return true;
// If element is smaller, search right half
else if (matrix[row][mid] < target) l = mid + 1;
// If element is larger, search left half
else r = mid - 1;
}

// If target not found in this row, return false
return false;
}
}

// If search exhausted all rows, return false
return false;
}

// Example usage:
const matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]]
const target = 3;
console.log(searchMatrix(matrix, target)); // Output: true