Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4263c30

Browse files
committed
Add solution #3091
1 parent 26f419f commit 4263c30

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,7 @@
21652165
3084|[Count Substrings Starting and Ending with Given Character](./solutions/3084-count-substrings-starting-and-ending-with-given-character.js)|Medium|
21662166
3085|[Minimum Deletions to Make String K-Special](./solutions/3085-minimum-deletions-to-make-string-k-special.js)|Medium|
21672167
3090|[Maximum Length Substring With Two Occurrences](./solutions/3090-maximum-length-substring-with-two-occurrences.js)|Easy|
2168+
3091|[Apply Operations to Make Sum of Array Greater Than or Equal to k](./solutions/3091-apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k.js)|Medium|
21682169
3105|[Longest Strictly Increasing or Strictly Decreasing Subarray](./solutions/3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js)|Easy|
21692170
3108|[Minimum Cost Walk in Weighted Graph](./solutions/3108-minimum-cost-walk-in-weighted-graph.js)|Hard|
21702171
3110|[Score of a String](./solutions/3110-score-of-a-string.js)|Easy|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 3091. Apply Operations to Make Sum of Array Greater Than or Equal to k
3+
* https://leetcode.com/problems/apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k/
4+
* Difficulty: Medium
5+
*
6+
* You are given a positive integer k. Initially, you have an array nums = [1].
7+
*
8+
* You can perform any of the following operations on the array any number of times (possibly zero):
9+
* - Choose any element in the array and increase its value by 1.
10+
* - Duplicate any element in the array and add it to the end of the array.
11+
*
12+
* Return the minimum number of operations required to make the sum of elements of the final array
13+
* greater than or equal to k.
14+
*/
15+
16+
/**
17+
* @param {number} k
18+
* @return {number}
19+
*/
20+
var minOperations = function(k) {
21+
if (k === 1) return 0;
22+
23+
let result = Infinity;
24+
for (let increments = 0; increments <= Math.ceil(Math.sqrt(k)); increments++) {
25+
const value = 1 + increments;
26+
const duplicates = Math.ceil(k / value) - 1;
27+
if (duplicates >= 0) {
28+
result = Math.min(result, increments + duplicates);
29+
}
30+
}
31+
32+
return result;
33+
};

0 commit comments

Comments
 (0)