Tap here to switch tabs
Problem
Submissions

Problem: Last Visited Integers

Last Visited Integers processes an array of positive values and negative one markers, returning the value at each marker's consecutive position among earlier values, or negative one when unavailable.

Statement

Given an integer array nums where each element is either a positive integer or −1-1.

You start with an empty array seen and an empty array ans. You iterate through nums from left to right:

  • If the current element is positive, prepend it to the front of seen and reset the count of consecutive −1-1 elements to 00.

  • If the current element is −1-1, increment the count of consecutive −1-1 elements by 11. Let this count be k.

    • If k is less than or equal to the length of seen, append the kk-th element of seen to ans.

    • Otherwise, if k is strictly greater than the length of seen, append −1-1 to ans.

Return the array ans.

Constraints:

  • 1≤1 \leq nums.length ≤100\leq 100

  • nums[i] =−1= -1 or 1≤1 \leq nums[i] ≤100\leq 100

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Last Visited Integers

Last Visited Integers processes an array of positive values and negative one markers, returning the value at each marker's consecutive position among earlier values, or negative one when unavailable.

Statement

Given an integer array nums where each element is either a positive integer or −1-1.

You start with an empty array seen and an empty array ans. You iterate through nums from left to right:

  • If the current element is positive, prepend it to the front of seen and reset the count of consecutive −1-1 elements to 00.

  • If the current element is −1-1, increment the count of consecutive −1-1 elements by 11. Let this count be k.

    • If k is less than or equal to the length of seen, append the kk-th element of seen to ans.

    • Otherwise, if k is strictly greater than the length of seen, append −1-1 to ans.

Return the array ans.

Constraints:

  • 1≤1 \leq nums.length ≤100\leq 100

  • nums[i] =−1= -1 or 1≤1 \leq nums[i] ≤100\leq 100