3066. Minimum Operations to Exceed Threshold Value II Medium
1/**
2 * [3066] Minimum Operations to Exceed Threshold Value II
3 *
4 * You are given a 0-indexed integer array nums, and an integer k.
5 * In one operation, you will:
6 *
7 * Take the two smallest integers x and y in nums.
8 * Remove x and y from nums.
9 * Add min(x, y) * 2 + max(x, y) anywhere in the array.
10 *
11 * Note that you can only apply the described operation if nums contains at least two elements.
12 * Return the minimum number of operations needed so that all elements of the array are greater than or equal to k.
13 *
14 * <strong class="example">Example 1:
15 *
16 * Input: nums = [2,11,10,1,3], k = 10
17 * Output: 2
18 * Explanation: In the first operation, we remove elements 1 and 2, then add 1 * 2 + 2 to nums. nums becomes equal to [4, 11, 10, 3].
19 * In the second operation, we remove elements 3 and 4, then add 3 * 2 + 4 to nums. nums becomes equal to [10, 11, 10].
20 * At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
21 * It can be shown that 2 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
22 *
23 * <strong class="example">Example 2:
24 *
25 * Input: nums = [1,1,2,4,9], k = 20
26 * Output: 4
27 * Explanation: After one operation, nums becomes equal to [2, 4, 9, 3].
28 * After two operations, nums becomes equal to [7, 4, 9].
29 * After three operations, nums becomes equal to [15, 9].
30 * After four operations, nums becomes equal to [33].
31 * At this stage, all the elements of nums are greater than 20 so we can stop.
32 * It can be shown that 4 is the minimum number of operations needed so that all elements of the array are greater than or equal to 20.
33 *
34 * Constraints:
35 *
36 * 2 <= nums.length <= 2 * 10^5
37 * 1 <= nums[i] <= 10^9
38 * 1 <= k <= 10^9
39 * The input is generated such that an answer always exists. That is, there exists some sequence of operations after which all elements of the array are greater than or equal to k.
40 *
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-ii/
45// discuss: https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-ii/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50 pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
51 0
52 }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_3066() {
63 }
64}
65
Back
© 2025 bowen.ge All Rights Reserved.