TGViewer
Python | LeetCode Python | LeetCode @easy_python_task · 9.03K subscribers
Post #2181 908
Задача: 1413. Minimum Value to Get Positive Step by Step Sum
Сложность: easy

Дан массив целых чисел nums, вы начинаете с начального положительного значения startValue.

На каждой итерации вы вычисляете поэтапную сумму startValue плюс элементы из nums (слева направо).

Верните минимальное положительное значение startValue, такое что поэтапная сумма никогда не будет меньше 1.

Пример:
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


👨‍💻 Алгоритм:

1⃣Инициализируйте переменные startValue со значением 1 и total со значением startValue.

2⃣Итеративно добавляйте каждый элемент массива nums к total и проверяйте, не опускается ли total ниже 1.

3⃣Если total падает ниже 1, увеличьте startValue на 1 и повторите шаги 2-3. Если total остается не менее 1, верните текущее значение startValue.

😎 Решение:
class 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


Ставь 👍 и забирай 📚 Базу знаний
  • 👍 1
More from @easy_python_task
  1. Oct 11, 2026Post #2439
  2. Oct 11, 2026Задача: 997. Find the Town Judge Сложность: easy В городе есть n человек, помеченных от 1…
  3. Oct 11, 2026Post #2437
  4. Oct 11, 2026Задача: 436. Find Right Interval Сложность: medium Дан массив интервалов, где intervals[i]…
  5. Oct 10, 2026Post #2435
  6. Oct 10, 2026Задача: 1249. Minimum Remove to Make Valid Parentheses Сложность: medium Дана строка s из…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →