2899. Last Visited Integers
Description
Given an integer array nums where nums[i] is either a positive integer or -1. We need to find for each -1 the respective positive integer, which we call the last visited integer.
To achieve this goal, let's define two empty arrays: seen and ans.
Start iterating from the beginning of the array nums.
- If a positive integer is encountered, prepend it to the front of
seen. - If
-1Β is encountered, letkbe the number of consecutive-1s seen so far (including the current-1),- If
kis less than or equal to the length ofseen, append thek-th element ofseentoans. - If
kis strictly greater than the length ofseen, append-1toans.
- If
Return the array ans.
Β
Example 1:
Input: nums = [1,2,-1,-1,-1]
Output: [2,1,-1]
Explanation:
Start with seen = [] and ans = [].
- Process
nums[0]: The first element in nums is1. We prepend it to the front ofseen. Now,seen == [1]. - Process
nums[1]: The next element is2. We prepend it to the front ofseen. Now,seen == [2, 1]. - Process
nums[2]: The next element is-1. This is the first occurrence of-1, sok == 1. We look for the first element in seen. We append2toans. Now,ans == [2]. - Process
nums[3]: Another-1. This is the second consecutive-1, sok == 2. The second element inseenis1, so we append1toans. Now,ans == [2, 1]. - Process
nums[4]: Another-1, the third in a row, makingk = 3. However,seenonly has two elements ([2, 1]). Sincekis greater than the number of elements inseen, we append-1toans. Finally,ans == [2, 1, -1].
Example 2:
Input: nums = [1,-1,2,-1,-1]
Output: [1,2,1]
Explanation:
Start with seen = [] and ans = [].
- Process
nums[0]: The first element in nums is1. We prepend it to the front ofseen. Now,seen == [1]. - Process
nums[1]: The next element is-1. This is the first occurrence of-1, sok == 1. We look for the first element inseen, which is1. Append1toans. Now,ans == [1]. - Process
nums[2]: The next element is2. Prepend this to the front ofseen. Now,seen == [2, 1]. - Process
nums[3]: The next element is-1. This-1is not consecutive to the first-1since2was in between. Thus,kresets to1. The first element inseenis2, so append2toans. Now,ans == [1, 2]. - Process
nums[4]: Another-1. This is consecutive to the previous-1, sok == 2. The second element inseenis1, append1toans. Finally,ans == [1, 2, 1].
Β
Constraints:
1 <= nums.length <= 100nums[i] == -1or1 <= nums[i]Β <= 100
Solutions
Solution 1: Simulation
We directly simulate according to the problem description.
Define an array \(\textit{seen}\) to store the positive integers we have encountered, and an array \(\textit{ans}\) to store the answer. We also need a variable \(k\) to record the number of consecutive \(-1\)s.
We traverse the array \(\textit{nums}\):
- If the current element \(x = -1\), we increment \(k\) by 1. If \(k\) is greater than the length of \(\textit{seen}\), we append \(-1\) to \(\textit{ans}\); otherwise, we append the \(k\)-th element from the end of \(\textit{seen}\) to \(\textit{ans}\).
- If the current element \(x\) is a positive integer, we reset \(k\) to 0 and append \(x\) to the end of \(\textit{seen}\).
The time complexity is \(O(n)\) and the space complexity is \(O(n)\), where \(n\) is the length of the array \(\textit{nums}\).
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |