Monitoring~15 min2s · 256 MBtarget O(n) time, O(1) spacestrings
A monitoring system writes one line for each request it serves. A line has two whole numbers, separated by one space: the bytes received and the bytes sent.
Your team lead wants the total of each column for a whole day. The day's log comes as one long text, with a line break after each line.
Write totals_from_text(text).
text holds the lines of the log. Each line is <received> <sent>, and a line break follows every line. The break after the last line may be missing.text means no requests.Input: text = "5 7\n10 20\n3 0\n"
Output: [18, 27]The text has three lines: 5 7, 10 20 and 3 0. The totals are added column by column.
| Line | Received | Sent | Total received | Total sent |
|---|---|---|---|---|
| 1 | 5 | 7 | 5 | 7 |
| 2 | 10 | 20 | 15 | 27 |
| 3 | 3 | 0 | 18 | 27 |
Input: text = ""
Output: [0, 0]An empty text has no lines, so both totals are 0.
Input: text = "100 0"
Output: [100, 0]One line, and no line break after it. The totals are 100 and 0.
0 ≤ length of text ≤ 2.5 x 10^6text has only digits, spaces and line breaksA line has at least 4 characters, so the text holds up to 6 x 10^5 lines. A solution that handles each line in constant time costs about 6 x 10^5 steps.
Cutting each line off the front of the text copies the rest of the text each time. That is about 6 x 10^5 x 2.5 x 10^6 / 2 characters, around 7.5 x 10^11, which is too slow. Read through the text once.
Each total can reach 6 x 10^5 x 10^9 = 6 x 10^14. Use a 64-bit total.