返回介绍

solution / 2100-2199 / 2113.Elements in Array After Removing and Replacing Elements / README_EN

发布于 2024-06-17 01:03:09 字数 5425 浏览 0 评论 0 收藏 0

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] if indexj < nums.length at minute timej
  • -1 if indexj >= nums.length at minute timej

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

class Solution:
  def elementInNums(self, nums: List[int], queries: List[List[int]]) -> List[int]:
    n, m = len(nums), len(queries)
    ans = [-1] * m
    for j, (t, i) in enumerate(queries):
      t %= 2 * n
      if t < n and i < n - t:
        ans[j] = nums[i + t]
      elif t > n and i < t - n:
        ans[j] = nums[i]
    return ans
class Solution {
  public int[] elementInNums(int[] nums, int[][] queries) {
    int n = nums.length, m = queries.length;
    int[] ans = new int[m];
    for (int j = 0; j < m; ++j) {
      ans[j] = -1;
      int t = queries[j][0], i = queries[j][1];
      t %= (2 * n);
      if (t < n && i < n - t) {
        ans[j] = nums[i + t];
      } else if (t > n && i < t - n) {
        ans[j] = nums[i];
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> elementInNums(vector<int>& nums, vector<vector<int>>& queries) {
    int n = nums.size(), m = queries.size();
    vector<int> ans(m, -1);
    for (int j = 0; j < m; ++j) {
      int t = queries[j][0], i = queries[j][1];
      t %= (n * 2);
      if (t < n && i < n - t) {
        ans[j] = nums[i + t];
      } else if (t > n && i < t - n) {
        ans[j] = nums[i];
      }
    }
    return ans;
  }
};
func elementInNums(nums []int, queries [][]int) []int {
  n, m := len(nums), len(queries)
  ans := make([]int, m)
  for j, q := range queries {
    t, i := q[0], q[1]
    t %= (n * 2)
    ans[j] = -1
    if t < n && i < n-t {
      ans[j] = nums[i+t]
    } else if t > n && i < t-n {
      ans[j] = nums[i]
    }
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文