2869. Minimum Operations to Collect Elements Easy
1/**
2 * [2869] Minimum Operations to Collect Elements
3 *
4 * You are given an array nums of positive integers and an integer k.
5 * In one operation, you can remove the last element of the array and add it to your collection.
6 * Return the minimum number of operations needed to collect elements 1, 2, ..., k.
7 *
8 * <strong class="example">Example 1:
9 *
10 * Input: nums = [3,1,5,4,2], k = 2
11 * Output: 4
12 * 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.
13 *
14 * <strong class="example">Example 2:
15 *
16 * Input: nums = [3,1,5,4,2], k = 5
17 * Output: 5
18 * 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.
19 *
20 * <strong class="example">Example 3:
21 *
22 * Input: nums = [3,2,5,3,1], k = 3
23 * Output: 4
24 * 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.
25 *
26 *
27 * Constraints:
28 *
29 * 1 <= nums.length <= 50
30 * 1 <= nums[i] <= nums.length
31 * 1 <= k <= nums.length
32 * The input is generated such that you can collect elements 1, 2, ..., k.
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/minimum-operations-to-collect-elements/
38// discuss: https://leetcode.com/problems/minimum-operations-to-collect-elements/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
44 0
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_2869() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.