A chain of ocean buoys reports one reading per hour to a shore station. Occasionally a transmission is lost and the hour is logged with the marker NA instead of a number. The station wants a clean, continuous log: whenever an hour is missing, it should show the most recent reading that was actually received before that hour. If an hour is missing and no reading has ever been received yet (the drop happened at or before the very first successful transmission), the station falls back to a fixed baseline value instead.
Given the hourly log and the baseline value, reconstruct the continuous log.
Line 1: two integers n and D — the number of hourly entries and the baseline default value.
Line 2: n whitespace-separated tokens. Each token is either the literal string NA (a dropped hour) or an integer (a successfully received reading).
A single line with n space-separated integers: the reconstructed log, in the original hour order.
NA token is an integer in [-10^6, 10^6]n tokens may be NA; all of them may be NA; none of them may be NAExample 1
Input
6 0 5 NA NA 8 NA 3
Expected
5 5 5 8 8 3
Explanation
Hours 2 and 3 are missing, so they carry forward the last received reading, 5. Hour 5 is missing and carries forward the last received reading, 8. The result is 5 5 5 8 8 3.
Example 2
Input
4 100 NA NA 7 NA
Expected
100 100 7 7
Explanation
The first two hours are missing before any reading has ever been received, so they fall back to the baseline value 100. Hour 3 is a real reading of 7. Hour 4 is missing and carries forward the most recent reading, 7. The result is 100 100 7 7.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →