-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply-operations-to-make-sum-of-array-greater-than-or-equal-to-k.html
50 lines (33 loc) · 1.94 KB
/
apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<p>You are given a <strong>positive</strong> integer <code>k</code>. Initially, you have an array <code>nums = [1]</code>.</p>
<p>You can perform <strong>any</strong> of the following operations on the array <strong>any</strong> number of times (<strong>possibly zero</strong>):</p>
<ul>
<li>Choose any element in the array and <strong>increase</strong> its value by <code>1</code>.</li>
<li>Duplicate any element in the array and add it to the end of the array.</li>
</ul>
<p>Return <em>the <strong>minimum</strong> number of operations required to make the <strong>sum</strong> of elements of the final array greater than or equal to </em><code>k</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">k = 11</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>We can do the following operations on the array <code>nums = [1]</code>:</p>
<ul>
<li>Increase the element by <code>1</code> three times. The resulting array is <code>nums = [4]</code>.</li>
<li>Duplicate the element two times. The resulting array is <code>nums = [4,4,4]</code>.</li>
</ul>
<p>The sum of the final array is <code>4 + 4 + 4 = 12</code> which is greater than or equal to <code>k = 11</code>.<br />
The total number of operations performed is <code>3 + 2 = 5</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>The sum of the original array is already greater than or equal to <code>1</code>, so no operations are needed.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>