3091. Apply Operations to Make Sum of Array Greater Than or Equal to k Medium
1/**
2 * [3091] Apply Operations to Make Sum of Array Greater Than or Equal to k
3 *
4 * You are given a positive integer k. Initially, you have an array nums = [1].
5 * You can perform any of the following operations on the array any number of times (possibly zero):
6 *
7 * Choose any element in the array and increase its value by 1.
8 * Duplicate any element in the array and add it to the end of the array.
9 *
10 * Return the minimum number of operations required to make the sum of elements of the final array greater than or equal to k.
11 *
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">k = 11</span>
15 * Output: <span class="example-io">5</span>
16 * Explanation:
17 * We can do the following operations on the array nums = [1]:
18 *
19 * Increase the element by 1 three times. The resulting array is nums = [4].
20 * Duplicate the element two times. The resulting array is nums = [4,4,4].
21 *
22 * The sum of the final array is 4 + 4 + 4 = 12 which is greater than or equal to k = 11.<br />
23 * The total number of operations performed is 3 + 2 = 5.
24 * </div>
25 * <strong class="example">Example 2:
26 * <div class="example-block">
27 * Input: <span class="example-io">k = 1</span>
28 * Output: <span class="example-io">0</span>
29 * Explanation:
30 * The sum of the original array is already greater than or equal to 1, so no operations are needed.
31 * </div>
32 *
33 * Constraints:
34 *
35 * 1 <= k <= 10^5
36 *
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k/
41// discuss: https://leetcode.com/problems/apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46 pub fn min_operations(k: i32) -> i32 {
47 0
48 }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn test_3091() {
59 }
60}
61
Back
© 2025 bowen.ge All Rights Reserved.