1413. Minimum Value to Get Positive Step by Step Sum
Дан arreglo целых чисел nums, вы начинаете с начального положительного значения startValue.
На каждой итерации вы вычисляете поэтапную сумму startValue плюс elementы из nums (слева направо).
return минимальное положительное значение startValue, такое что поэтапная сумма никогда не будет меньше 1.
Ejemplo:
Input: nums = [-3,2,-3,4,2]
Output: 5
Explanation: If you choose startValue = 4, in the third iteration your step by step sum is less than 1.
step by step sum
startValue = 4 | startValue = 5 | nums
(4 -3 ) = 1 | (5 -3 ) = 2 | -3
(1 +2 ) = 3 | (2 +2 ) = 4 | 2
(3 -3 ) = 0 | (4 -3 ) = 1 | -3
(0 +4 ) = 4 | (1 +4 ) = 5 | 4
(4 +2 ) = 6 | (5 +2 ) = 7 | 2
C# solución
coincidente/originalpublic class Solution {
public int MinStartValue(int[] nums) {
int startValue = 1;
while (true) {
int total = startValue;
bool isValid = true;
foreach (int num in nums) {
total += num;
if (total < 1) {
isValid = false;
break;
}
}
if (isValid) {
return startValue;
}
startValue += 1;
}
}
}
C++ solución
borrador automático, revisar antes de enviar#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
public int MinStartValue(vector<int>& nums) {
int startValue = 1;
while (true) {
int total = startValue;
bool isValid = true;
foreach (int num in nums) {
total += num;
if (total < 1) {
isValid = false;
break;
}
}
if (isValid) {
return startValue;
}
startValue += 1;
}
}
}
Java solución
coincidente/originalclass Solution {
public int minStartValue(int[] nums) {
int startValue = 1;
while (true) {
int total = startValue;
boolean isValid = true;
for (int num : nums) {
total += num;
if (total < 1) {
isValid = false;
break;
}
}
if (isValid) {
return startValue;
} else {
startValue += 1;
}
}
}
}
JavaScript solución
coincidente/originalvar minStartValue = function(nums) {
let startValue = 1;
while (true) {
let total = startValue;
let isValid = true;
for (let num of nums) {
total += num;
if (total < 1) {
isValid = false;
break;
}
}
if (isValid) {
return startValue;
}
startValue++;
}
};
Python solución
coincidente/originalclass Solution:
def minStartValue(self, nums: List[int]) -> int:
startValue = 1
while True:
total = startValue
isValid = True
for num in nums:
total += num
if total < 1:
isValid = False
break
if isValid:
return startValue
startValue += 1
Go solución
coincidente/originalfunc minStartValue(nums []int) int {
startValue := 1
for {
total := startValue
isValid := true
for _, num := range nums {
total += num
if total < 1 {
isValid = false
break
}
}
if isValid {
return startValue
}
startValue++
}
}
Algorithm
Инициализируйте переменные startValue со значением 1 и total со значением startValue.
Итеративно добавляйте каждый element arregloа nums к total и проверяйте, не опускается ли total ниже 1.
Если total падает ниже 1, увеличьте startValue на 1 и повторите шаги 2-3. Если total остается не менее 1, return текущее значение startValue.
😎
Vacantes para esta tarea
Se muestran vacantes activas con etiquetas coincidentes.