返回介绍

solution / 2800-2899 / 2869.Minimum Operations to Collect Elements / README_EN

发布于 2024-06-17 01:02:59 字数 4514 浏览 0 评论 0 收藏 0

2869. Minimum Operations to Collect Elements

中文文档

Description

You are given an array nums of positive integers and an integer k.

In one operation, you can remove the last element of the array and add it to your collection.

Return _the minimum number of operations needed to collect elements_ 1, 2, ..., k.

 

Example 1:

Input: nums = [3,1,5,4,2], k = 2
Output: 4
Explanation: After 4 operations, we collect elements 2, 4, 5, and 1, in this order. Our collection contains elements 1 and 2. Hence, the answer is 4.

Example 2:

Input: nums = [3,1,5,4,2], k = 5
Output: 5
Explanation: After 5 operations, we collect elements 2, 4, 5, 1, and 3, in this order. Our collection contains elements 1 through 5. Hence, the answer is 5.

Example 3:

Input: nums = [3,2,5,3,1], k = 3
Output: 4
Explanation: After 4 operations, we collect elements 1, 3, 5, and 2, in this order. Our collection contains elements 1 through 3. Hence, the answer is 4.

 

Constraints:

  • 1 <= nums.length <= 50
  • 1 <= nums[i] <= nums.length
  • 1 <= k <= nums.length
  • The input is generated such that you can collect elements 1, 2, ..., k.

Solutions

Solution 1: Traverse in Reverse Order

We can traverse the array in reverse order. For each element encountered during the traversal that is less than or equal to $k$ and has not been added to the set yet, we add it to the set until the set contains elements from $1$ to $k$.

The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(k)$.

class Solution:
  def minOperations(self, nums: List[int], k: int) -> int:
    is_added = [False] * k
    count = 0
    n = len(nums)
    for i in range(n - 1, -1, -1):
      if nums[i] > k or is_added[nums[i] - 1]:
        continue
      is_added[nums[i] - 1] = True
      count += 1
      if count == k:
        return n - i
class Solution {
  public int minOperations(List<Integer> nums, int k) {
    boolean[] isAdded = new boolean[k];
    int n = nums.size();
    int count = 0;
    for (int i = n - 1;; i--) {
      if (nums.get(i) > k || isAdded[nums.get(i) - 1]) {
        continue;
      }
      isAdded[nums.get(i) - 1] = true;
      count++;
      if (count == k) {
        return n - i;
      }
    }
  }
}
class Solution {
public:
  int minOperations(vector<int>& nums, int k) {
    int n = nums.size();
    vector<bool> isAdded(n);
    int count = 0;
    for (int i = n - 1;; --i) {
      if (nums[i] > k || isAdded[nums[i] - 1]) {
        continue;
      }
      isAdded[nums[i] - 1] = true;
      if (++count == k) {
        return n - i;
      }
    }
  }
};
func minOperations(nums []int, k int) int {
  isAdded := make([]bool, k)
  count := 0
  n := len(nums)
  for i := n - 1; ; i-- {
    if nums[i] > k || isAdded[nums[i]-1] {
      continue
    }
    isAdded[nums[i]-1] = true
    count++
    if count == k {
      return n - i
    }
  }
}
function minOperations(nums: number[], k: number): number {
  const n = nums.length;
  const isAdded = Array(k).fill(false);
  let count = 0;
  for (let i = n - 1; ; --i) {
    if (nums[i] > k || isAdded[nums[i] - 1]) {
      continue;
    }
    isAdded[nums[i] - 1] = true;
    ++count;
    if (count === k) {
      return n - i;
    }
  }
}

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

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

发布评论

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