Loading
Loading
An accounting tool keeps transaction amounts in a grid. Each row is sorted in increasing order from left to right, and each column is sorted in increasing order from top to bottom.
Return true if the amount target appears anywhere in ledger, and false otherwise.
ledger = [[1,4,7,11],[2,5,8,12],[3,6,9,16],[10,13,14,17]], target = 9(0,3) = 1111 > 9: the whole column below is bigger too — move left(0,2) = 77 < 9: the whole row to the left is smaller too — move down(1,2) = 88 < 9 — move down(2,2) = 9foundtrueSign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
| 0 | 1 | 4 | 7 | 11 |
|---|---|---|---|---|
| 1 | 2 | 5 | 8 | 12 |
| 2 | 3 | 6 | 9 | 16 |
| 3 | 10 | 13 | 14 | 17 |
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(rows + cols) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
| 0 | 1 | 4 | 7 | 11 |
|---|---|---|---|---|
| 1 | 2 | 5 | 8 | 12 |
| 2 | 3 | 6 | 9 | 16 |
| 3 | 10 | 13 | 14 | 17 |
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Example 2
| 0 | 1 | 4 | 7 | 11 |
|---|---|---|---|---|
| 1 | 2 | 5 | 8 | 12 |
| 2 | 3 | 6 | 9 | 16 |
| 3 | 10 | 13 | 14 | 17 |