当前位置:网站首页>Day10 daily 3 questions (1): sum gradually to get the minimum value of positive numbers
Day10 daily 3 questions (1): sum gradually to get the minimum value of positive numbers
2022-06-26 16:36:00 【Programmed ape without hair loss 2】
subject :
Give you an array of integers nums . You can choose any Positive numbers startValue As an initial value .
You need to traverse from left to right nums Array , And will startValue Add up in sequence nums The values in the array .
Please make sure that the cumulative sum is always greater than or equal to 1 Under the premise of , Choose the smallest Positive numbers As startValue .
Example 1:
Input :nums = [-3,2,-3,4,2]
Output :5
explain : If you choose startValue = 4, At the third cumulation , And less than 1 .
Sum by accumulation
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
Example 2:
Input :nums = [1,2]
Output :1
explain : The smallest startValue Positive number expected .
Example 3:
Input :nums = [1,-2,-3]
Output :5
Tips :
1 <= nums.length <= 100
-100 <= nums[i] <= 100
Ideas :
because startValue Positive number , therefore startValue>0, And because every element of the accumulation must >0, therefore startValue The initial value of can be
Math.max(1,-nums[0]+1);
Traverse nums, When you add up and <=0 when ,start+=1-sum, At this time, the cumulative sum is also more 1-sum, namely sum+=1-sum, namely sum=1;
java Code :
class Solution {
public int minStartValue(int[] nums) {
int start = Math.max(1, -nums[0] + 1);
int sum = start;
for (int i : nums) {
sum += i;
if (sum <= 0) {
start += 1 - sum;
sum = 1;
}
}
return start;
}
}
边栏推荐
- Cookie和Session详解
- day10每日3题(2):统计最大组的数目
- 【力扣刷题】单调栈:84. 柱状图中最大的矩形
- [learn FPGA programming from scratch -46]: Vision - development and technological progress of integrated circuits
- Scala 基础 (二):变量和数据类型
- 架构实战营毕业设计
- TCP拥塞控制详解 | 1. 概述
- Codeforces Round #802 (Div. 2)
- 心情不好,我就这样写代码
- Least squares system identification class II: recursive least squares
猜你喜欢

Oilfield exploration problems

When a programmer is disturbed 10 times a day, the consequences are amazing!

MS|谢黎炜组发现混合益生菌制剂及其代谢产物可缓解结肠炎

Cloud platform monitoring system based on stm32+ Huawei cloud IOT design

JUnit unit test

Research on natural transition dubbing processing scheme based on MATLAB

国内首款开源 MySQL HTAP 数据库即将发布,三大看点提前告知
![[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity](/img/e8/9c5f1658a252c3c80503b5021917f6.jpg)
[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity

精致妆容成露营“软实力”,唯品会户外美妆护肤产品销量激增

What is the process of switching C # read / write files from user mode to kernel mode?
随机推荐
[from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)
How to separate jar packages and resource files according to packaging?
C语言 头哥习题答案截图
Redis migration (recommended operation process)
Net based on girdview control to delete and edit row data
Greenplum database fault analysis - semop (id=2000421076, num=11) failed: invalid argument
Tsinghua's "magic potion" is published in nature: reversing stem cell differentiation, and the achievements of the Nobel Prize go further. Netizen: life can be created without sperm and eggs
【207】Apache崩溃的几个很可能的原因,apache崩溃几个
100+ data science interview questions and answers Summary - basic knowledge and data analysis
架构实战营毕业设计
《软件工程》期末重点复习笔记
Pybullet robot simulation environment construction 5 Robot pose visualization
R language plot visualization: plot visualizes the normalized histogram, adds the density curve KDE to the histogram, and uses geom at the bottom edge of the histogram_ Adding edge whisker graph with
Science | 红树林中发现的巨型细菌挑战传统无核膜观念
Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's Mae
并发编程整体脉络
What is the process of switching C # read / write files from user mode to kernel mode?
Stm32f103c8t6 realize breathing lamp code
若依微服务特殊字符串被过滤的解决办法
Binary array command of redis