Talentd
FleetCode

3091. Apply Operations to Make Sum of Array Greater Than or Equal to k

Medium44.2% acceptance2 companies

You are given a positive integer k. Initially, you have an array nums = [1].

You can perform any of the following operations on the array any number of times (possibly zero):

  • Choose any element in the array and increase its value by 1.
  • Duplicate any element in the array and add it to the end of the array.

Return the minimum number of operations required to make the sum of elements of the final array greater than or equal to k.

Example 1:

Input: k = 11

Output: 5

Explanation:

We can do the following operations on the array nums = [1]:

  • Increase the element by 1 three times. The resulting array is nums = [4].
  • Duplicate the element two times. The resulting array is nums = [4,4,4].

The sum of the final array is 4 + 4 + 4 = 12 which is greater than or equal to k = 11.
The total number of operations performed is 3 + 2 = 5.

Example 2:

Input: k = 1

Output: 0

Explanation:

The sum of the original array is already greater than or equal to 1, so no operations are needed.

Constraints:

  • 1 <= k <= 105

Asked at 2 companies

Hints open one at a time

Hint 1
It is optimal to make all the increase operations first and all the duplicate operations last.
Hint 2
Iterate over all possible number of increase operations that can be done and find the corresponding number of duplicate operations.
22:21
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def minOperations(self, k: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
11
Input
1
Your code is saved on this device.Solution