2113. Elements in Array After Removing and Replacing Elements π
Description
You are given a 0-indexed integer array nums
. Initially on minute 0
, the array is unchanged. Every minute, the leftmost element in nums
is removed until no elements remain. Then, every minute, one element is appended to the end of nums
, in the order they were removed in, until the original array is restored. This process repeats indefinitely.
- For example, the array
[0,1,2]
would change as follows:[0,1,2] → [1,2] → [2] → [] → [0] → [0,1] → [0,1,2] → [1,2] → [2] → [] → [0] → [0,1] → [0,1,2] → ...
You are also given a 2D integer array queries
of size n
where queries[j] = [timej, indexj]
. The answer to the jth
query is:
nums[indexj]
ifindexj < nums.length
at minutetimej
-1
ifindexj >= nums.length
at minutetimej
Return an integer array ans
of size n
where ans[j]
is the answer to the jth
query.
Example 1:
Input: nums = [0,1,2], queries = [[0,2],[2,0],[3,2],[5,0]] Output: [2,2,-1,0] Explanation: Minute 0: [0,1,2] - All elements are in the nums. Minute 1: [1,2] - The leftmost element, 0, is removed. Minute 2: [2] - The leftmost element, 1, is removed. Minute 3: [] - The leftmost element, 2, is removed. Minute 4: [0] - 0 is added to the end of nums. Minute 5: [0,1] - 1 is added to the end of nums. At minute 0, nums[2] is 2. At minute 2, nums[0] is 2. At minute 3, nums[2] does not exist. At minute 5, nums[0] is 0.
Example 2:
Input: nums = [2], queries = [[0,0],[1,0],[2,0],[3,0]] Output: [2,-1,2,-1] Minute 0: [2] - All elements are in the nums. Minute 1: [] - The leftmost element, 2, is removed. Minute 2: [2] - 2 is added to the end of nums. Minute 3: [] - The leftmost element, 2, is removed. At minute 0, nums[0] is 2. At minute 1, nums[0] does not exist. At minute 2, nums[0] is 2. At minute 3, nums[0] does not exist.
Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 100
n == queries.length
1 <= n <= 105
queries[j].length == 2
0 <= timej <= 105
0 <= indexj < nums.length
Solutions
Solution 1: Direct Calculation
First, we initialize an array $ans$ with length $m$ to store the answers, initializing all elements to $-1$.
Next, we iterate through the array $queries$. For each query, we first obtain the current query time $t$ and index $i$. We then take $t$ modulo $2n$ and compare $t$ with $n$:
- If $t < n$, then the number of array elements at time $t$ is $n - t$, and the array elements are the result of the original array elements shifted left by $t$ positions. Therefore, if $i < n - t$, the answer is $nums[i + t]$;
- If $t > n$, then the number of array elements at time $t$ is $t - n$, and the array elements are the first $t - n$ elements of the original array. Therefore, if $i < t - n$, the answer is $nums[i]$.
Finally, return the array $ans$.
The time complexity is $O(m)$, where $m$ is the length of the array $queries$. Ignoring the space consumed by the answer array, the space complexity is $O(1)$.
1 2 3 4 5 6 7 8 9 10 11 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|