2899. Last Visited Integers Easy

@problem@discussion
#Array#Simulation



1/**
2 * [2899] Last Visited Integers
3 *
4 * 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.
5 * To achieve this goal, let's define two empty arrays: seen and ans.
6 * Start iterating from the beginning of the array nums.
7 * 
8 * 	If a positive integer is encountered, prepend it to the front of seen.
9 * 	If -1 is encountered, let k be the number of consecutive -1s seen so far (including the current -1),
10 * 	
11 * 		If k is less than or equal to the length of seen, append the k-th element of seen to ans.
12 * 		If k is strictly greater than the length of seen, append -1 to ans.
13 * 	
14 * 	
15 * 
16 * Return the array ans.
17 *  
18 * <strong class="example">Example 1:
19 * <div class="example-block">
20 * Input: <span class="example-io">nums = [1,2,-1,-1,-1]</span>
21 * Output: <span class="example-io">[2,1,-1]</span>
22 * Explanation:
23 * Start with seen = [] and ans = [].
24 * <ol>
25 * 	Process nums[0]: The first element in nums is 1. We prepend it to the front of seen. Now, seen == [1].
26 * 	Process nums[1]: The next element is 2. We prepend it to the front of seen. Now, seen == [2, 1].
27 * 	Process nums[2]: The next element is -1. This is the first occurrence of -1, so k == 1. We look for the first element in seen. We append 2 to ans. Now, ans == [2].
28 * 	Process nums[3]: Another -1. This is the second consecutive -1, so k == 2. The second element in seen is 1, so we append 1 to ans. Now, ans == [2, 1].
29 * 	Process nums[4]: Another -1, the third in a row, making k = 3. However, seen only has two elements ([2, 1]). Since k is greater than the number of elements in seen, we append -1 to ans. Finally, ans == [2, 1, -1].
30 * </ol>
31 * </div>
32 * <strong class="example">Example 2:
33 * <div class="example-block">
34 * Input: <span class="example-io">nums = [1,-1,2,-1,-1]</span>
35 * Output:<span class="example-io"> [1,2,1]</span>
36 * Explanation:
37 * Start with seen = [] and ans = [].
38 * <ol>
39 * 	Process nums[0]: The first element in nums is 1. We prepend it to the front of seen. Now, seen == [1].
40 * 	Process nums[1]: The next element is -1. This is the first occurrence of -1, so k == 1. We look for the first element in seen, which is 1. Append 1 to ans. Now, ans == [1].
41 * 	Process nums[2]: The next element is 2. Prepend this to the front of seen. Now, seen == [2, 1].
42 * 	Process nums[3]: The next element is -1. This -1 is not consecutive to the first -1 since 2 was in between. Thus, k resets to 1. The first element in seen is 2, so append 2 to ans. Now, ans == [1, 2].
43 * 	Process nums[4]: Another -1. This is consecutive to the previous -1, so k == 2. The second element in seen is 1, append 1 to ans. Finally, ans == [1, 2, 1].
44 * </ol>
45 * </div>
46 *  
47 * Constraints:
48 * 
49 * 	1 <= nums.length <= 100
50 * 	nums[i] == -1 or 1 <= nums[i] <= 100
51 * 
52 */
53pub struct Solution {}
54
55// problem: https://leetcode.com/problems/last-visited-integers/
56// discuss: https://leetcode.com/problems/last-visited-integers/discuss/?currentPage=1&orderBy=most_votes&query=
57
58// submission codes start here
59
60impl Solution {
61    pub fn last_visited_integers(nums: Vec<i32>) -> Vec<i32> {
62        vec![]
63    }
64}
65
66// submission codes end
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn test_2899() {
74    }
75}
76


Back
© 2025 bowen.ge All Rights Reserved.