Skip to content

Commit

Permalink
feat(search-in-sorted-matrix): add solution 2
Browse files Browse the repository at this point in the history
  • Loading branch information
filipe1309 committed Nov 19, 2023
1 parent f2cebdd commit c21cdd6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/medium/search-in-sorted-matrix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,23 @@ target = 44

<details>
<summary>Hint 1</summary>
...
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?
</details>

<details>
<summary>Hint 2</summary>
...
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.
</details>

<details>
<summary>Optimal Space &amp; Time Complexity</summary>
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
</details>
3 changes: 2 additions & 1 deletion src/medium/search-in-sorted-matrix/solution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
1 change: 1 addition & 0 deletions src/medium/search-in-sorted-matrix/solutions/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './solution-0';
export * from './solution-1';
export * from './solution-2';
15 changes: 15 additions & 0 deletions src/medium/search-in-sorted-matrix/solutions/solution-2.ts
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit c21cdd6

Please sign in to comment.