diff --git a/src/medium/search-in-sorted-matrix/README.md b/src/medium/search-in-sorted-matrix/README.md index ae5b2ef..b839991 100644 --- a/src/medium/search-in-sorted-matrix/README.md +++ b/src/medium/search-in-sorted-matrix/README.md @@ -33,15 +33,23 @@ target = 44
Hint 1 -... +Pick any number in the matrix and compare it to the target number. If this number +is bigger than the target number, what does that tell you about all of the other +numbers in this number's row and this number's column? What about if this number +is smaller than the target number?
Hint 2 -... +Try starting at the top right corner of the matrix, comparing the number there to +the target number, and using whatever you gathered from Hint #1 to figure out +what number to compare next if the top right number isn't equal to the target +number. Continue until you find the target number or until you get past the extremities +of the matrix.
Optimal Space & Time Complexity -O(??) time | O(??) space - where ?? is ... +O(n + m) time | O(1) space - where n is the length of the matrix's rows and m is +the length of the matrix's columns
diff --git a/src/medium/search-in-sorted-matrix/solution.spec.ts b/src/medium/search-in-sorted-matrix/solution.spec.ts index 12c9ae4..ac24460 100644 --- a/src/medium/search-in-sorted-matrix/solution.spec.ts +++ b/src/medium/search-in-sorted-matrix/solution.spec.ts @@ -3,12 +3,13 @@ import cases from './cases'; import { solution0, // O(h * w) time | O(1) space solution1, // O(h * log(w)) time | O(1) space + solution2, // O(h + w) time | O(1) space } from "./solutions"; // Test: make test t=search-in-sorted-matrix describe('search-in-sorted-matrix', () => { test.each(cases)('%# (%j)', ({ input, expected }) => { - const result = solution1(input.matrix, input.target); + const result = solution2(input.matrix, input.target); expect(result).toEqual(expected); }); }); diff --git a/src/medium/search-in-sorted-matrix/solutions/index.ts b/src/medium/search-in-sorted-matrix/solutions/index.ts index c157424..c3c5b45 100644 --- a/src/medium/search-in-sorted-matrix/solutions/index.ts +++ b/src/medium/search-in-sorted-matrix/solutions/index.ts @@ -1,2 +1,3 @@ export * from './solution-0'; export * from './solution-1'; +export * from './solution-2'; diff --git a/src/medium/search-in-sorted-matrix/solutions/solution-2.ts b/src/medium/search-in-sorted-matrix/solutions/solution-2.ts new file mode 100644 index 0000000..5eb2b2f --- /dev/null +++ b/src/medium/search-in-sorted-matrix/solutions/solution-2.ts @@ -0,0 +1,15 @@ +// Adjust Row/Col Index (2 Pointers) approach +// Complexity (worst-case): O(w + h) time | O(1) space +function searchInSortedMatrix(matrix: number[][], target: number): RangeMatrix { + let row = 0; + let col = matrix[0].length - 1; + + while (row < matrix.length && col >= 0) { + if (matrix[row][col] > target) col--; + else if (matrix[row][col] < target) row++; + else return [row, col]; + } + + return [-1, -1]; +} +export { searchInSortedMatrix as solution2 };