Ad platform~10 min2s · 256 MBtarget O(n) time, O(1) spacetraversal
An ad platform writes one bid on each line of a text log. A bid is a whole number of units. Some lines are blank, because a writer sometimes flushes an empty line.
The file has no header, so nobody tells you how many lines it holds. Your program must read until the lines run out.
Write read_all_lines(lines).
lines holds every line of the log, in order. A line is either blank (an empty string) or a whole number written in digits.Input: lines = ["12", "", "7", "100"]
Output: [3, 119]Three lines hold a bid. The blank line is skipped, and reading goes on after it.
| Line | Text | Bids read so far | Total so far |
|---|---|---|---|
| 1 | 12 |
1 | 12 |
| 2 | blank | 1 | 12 |
| 3 | 7 |
2 | 19 |
| 4 | 100 |
3 | 119 |
Input: lines = []
Output: [0, 0]No lines: nothing was read, so the answer is [0, 0].
Input: lines = ["0"]
Output: [1, 0]One bid of 0 counts as a bid. The answer is [1, 0].
0 ≤ length of lines ≤ 3 x 10^5A bid can be 999,999,999, so 300,000 bids can total about 3 x 10^14. That is far beyond a 32-bit integer. Use a 64-bit total.
With 3 x 10^5 lines, a loop that moves every other line for each line it reads costs about 4.5 x 10^10 moves. Walk the lines in place instead.