diff --git a/README.md b/README.md index 2bf93af3..95d06996 100644 --- a/README.md +++ b/README.md @@ -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| diff --git a/algorithms/ts/search2DMatrix/search-2d-matrix.ts b/algorithms/ts/search2DMatrix/search-2d-matrix.ts new file mode 100644 index 00000000..68b2fbe2 --- /dev/null +++ b/algorithms/ts/search2DMatrix/search-2d-matrix.ts @@ -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 \ No newline at end of file