#2899

Last Visited Integers

pupil · 415 · lc easy +25 · 62.1% accepted · 165 likes · top 63%

Description

An integer array nums is given where each nums[i] is either a positive integer or -1. Find for each -1 the corresponding last visited integer using the following procedure:

Maintain two arrays seen (initially empty) and ans.

Iterate through nums:

- Positive integer: prepend it to seen.

- -1: let k be the count of consecutive -1s so far (including this one).

- If k <= len(seen), append seen[k - 1] to ans.

- Otherwise, append -1 to ans.

Return ans.

Code

1
2
3