2869. Minimum Operations to Collect Elements LeetCode Solution

In this guide, you will get 2869. Minimum Operations to Collect Elements LeetCode Solution with the best time and space complexity. The solution to Minimum Operations to Collect Elements problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Operations to Collect Elements solution in C++
  4. Minimum Operations to Collect Elements solution in Java
  5. Minimum Operations to Collect Elements solution in Python
  6. Additional Resources
2869. Minimum Operations to Collect Elements LeetCode Solution image

Problem Statement of Minimum Operations to Collect Elements

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.

See also  2438. Range Product Queries of Powers LeetCode Solution

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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(k)

2869. Minimum Operations to Collect Elements LeetCode Solution in C++

class Solution {
 public:
  int minOperations(vector<int>& nums, int k) {
    unordered_set<int> seen;

    for (int i = nums.size() - 1; i >= 0; --i) {
      if (nums[i] > k)
        continue;
      seen.insert(nums[i]);
      if (seen.size() == k)
        return nums.size() - i;
    }

    throw;
  }
};
/* code provided by PROGIEZ */

2869. Minimum Operations to Collect Elements LeetCode Solution in Java

class Solution {
  public int minOperations(List<Integer> nums, int k) {
    Set<Integer> seen = new HashSet<>();

    for (int i = nums.size() - 1; i >= 0; --i) {
      if (nums.get(i) > k)
        continue;
      seen.add(nums.get(i));
      if (seen.size() == k)
        return nums.size() - i;
    }

    throw new IllegalArgumentException();
  }
}
// code provided by PROGIEZ

2869. Minimum Operations to Collect Elements LeetCode Solution in Python

class Solution:
  def minOperations(self, nums: list[int], k: int) -> int:
    seen = set()

    for i, num in enumerate(reversed(nums)):
      if num > k:
        continue
      seen.add(num)
      if len(seen) == k:
        return i + 1
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.