Advertisement

Last Visited Integers - LeetCode 2899 Solution

Last Visited Integers - Complete Solution Guide

Last Visited Integers is LeetCode problem 2899, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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, let k be the number of consecutive -1 s seen so far (including the current -1 ), If k is less than or e

Detailed Explanation

The problem asks to process an integer array `nums`. Each element is either a positive integer or -1. For each -1 encountered, we need to find the corresponding 'last visited' positive integer. We maintain a list `seen` storing positive integers encountered so far in reverse order of appearance (most recently seen at the front). When a -1 is encountered, we check how many consecutive -1s have been seen. If this count `k` is less than or equal to the number of elements in `seen`, the k-th element in `seen` is the 'last visited' integer. Otherwise, if `k` exceeds the size of `seen`, the 'last visited' is -1. The output is a list `ans` containing these 'last visited' integers.

Solution Approach

The solution uses a `seen` list to store the positive integers encountered so far in reverse order of their appearance. A `k` variable counts consecutive -1s. The algorithm iterates through `nums`. If a positive integer is found, it's added to the beginning of `seen`, and `k` is reset. If a -1 is found, `k` is incremented. The k-th element of `seen` (or -1 if k exceeds `seen`'s size) is appended to the `ans` list.

Step-by-Step Algorithm

  1. Initialize an empty list `seen` to store positive integers in reverse order of appearance and an empty list `ans` to store the results.
  2. Initialize `k` to 0 to count consecutive -1s.
  3. Iterate through the input array `nums`:
  4. If the current element `num` is positive, prepend it to `seen` and reset `k` to 0.
  5. If `num` is -1, increment `k`. If `k` is less than or equal to the length of `seen`, append the `k`-th element of `seen` to `ans`. Otherwise, append -1 to `ans`.
  6. Return the `ans` list.

Key Insights

  • Using a deque (or list with `insert(0, num)`) for `seen` efficiently handles prepending new positive integers.
  • The variable `k` tracks consecutive -1s, resetting when a positive integer is encountered. This is crucial for correctly identifying the 'last visited' integer.
  • Handling the case where `k` exceeds `seen`'s length (no 'last visited' positive integer within the window of consecutive -1s) requires appending -1 to the result.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n)

Topics

This problem involves: Array, Simulation.

Companies

Asked at: General Motors.