Skip to content

Commit 1c8473f

Browse files
committed
Add solution #3511
1 parent 75c5075 commit 1c8473f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,6 +2713,7 @@
27132713
3491|[Phone Number Prefix](./solutions/3491-phone-number-prefix.js)|Easy|
27142714
3496|[Maximize Score After Pair Deletions](./solutions/3496-maximize-score-after-pair-deletions.js)|Medium|
27152715
3506|[Find Time Required to Eliminate Bacterial Strains](./solutions/3506-find-time-required-to-eliminate-bacterial-strains.js)|Hard|
2716+
3511|[Make a Positive Array](./solutions/3511-make-a-positive-array.js)|Medium|
27162717

27172718
## License
27182719

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 3511. Make a Positive Array
3+
* https://leetcode.com/problems/make-a-positive-array/
4+
* Difficulty: Medium
5+
*
6+
* You are given an array nums. An array is considered positive if the sum of all numbers in
7+
* each subarray with more than two elements is positive.
8+
*
9+
* You can perform the following operation any number of times:
10+
* - Replace one element in nums with any integer between -1018 and 1018.
11+
*
12+
* Find the minimum number of operations needed to make nums positive.
13+
*/
14+
15+
/**
16+
* @param {number[]} nums
17+
* @return {number}
18+
*/
19+
var makeArrayPositive = function(nums) {
20+
let result = 0;
21+
let lowestSum = nums[0] + nums[1];
22+
23+
for (let i = 2; i < nums.length; i++) {
24+
const currentValue = nums[i];
25+
const currentTriplet = currentValue + nums[i - 1] + nums[i - 2];
26+
lowestSum = Math.min(lowestSum + currentValue, currentTriplet);
27+
28+
if (lowestSum <= 0) {
29+
nums[i] = lowestSum = 10 ** 18;
30+
result++;
31+
}
32+
}
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy