当前位置:网站首页>day10每日3题(1):逐步求和得到正数的最小值
day10每日3题(1):逐步求和得到正数的最小值
2022-06-26 16:17:00 【程序猿不脱发2】
题目:
给你一个整数数组 nums 。你可以选定任意的 正数 startValue 作为初始值。
你需要从左到右遍历 nums 数组,并将 startValue 依次累加上 nums 数组中的值。
请你在确保累加和始终大于等于 1 的前提下,选出一个最小的 正数 作为 startValue 。
示例 1:
输入:nums = [-3,2,-3,4,2]
输出:5
解释:如果你选择 startValue = 4,在第三次累加时,和小于 1 。
累加求和
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
示例 2:
输入:nums = [1,2]
输出:1
解释:最小的 startValue 需要是正数。
示例 3:
输入:nums = [1,-2,-3]
输出:5
提示:
1 <= nums.length <= 100
-100 <= nums[i] <= 100
思路:
由于startValue是正数,因此startValue>0, 又因为累加的每个元素都要>0,所以startValue的初始值可以为
Math.max(1,-nums[0]+1);
遍历nums, 当累加和<=0时,start+=1-sum, 此时累加和也多出1-sum, 即sum+=1-sum, 即sum=1;
java代码:
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;
}
}
边栏推荐
- JS教程之 使用 Electron.JS 构建原生桌面应用程序乒乓游戏
- 手写数字体识别,用保存的模型跑自己的图片
- (一)keras手写数字体识别并识别自己写的数字
- Failed to get convolution algorithm. This is probably because cuDNN failed to initialize
- 【从删库到跑路】MySQL基础 完结篇(入个门先跑路了。。)
- Anaconda3安装tensorflow 2.0版本cpu和gpu安装,Win10系统
- 固件供应链公司Binarly获得WestWave Capital和Acrobator Ventures的360万美元投资
- 【从删库到跑路】JDBC 完结篇(一天学完系列!!学完赶紧跑!)
- 01 backpack DP
- JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
猜你喜欢

Stepn novice introduction and advanced

我把它当副业月入3万多,新手月入过万的干货分享!
![[graduation season] a word for graduates: the sky is high enough for birds to fly, and the sea is wide enough for fish to leap](/img/b6/21e51fa7f79d4a4b950f061703f0fb.png)
[graduation season] a word for graduates: the sky is high enough for birds to fly, and the sea is wide enough for fish to leap

I regard it as a dry product with a monthly income of more than 30000 yuan for sidelines and more than 10000 yuan for novices!

stm32h7b0替代h750程序导致单片机挂掉无法烧录程序问题

用Attention和微调BERT进行自然语言推断-PyTorch

振动式液量检测装置

This year, the AI score of college entrance examination English is 134. The research of Fudan Wuda alumni is interesting

3. Keras version model training

【毕业季】致毕业生的一句话:天高任鸟飞,海阔凭鱼跃
随机推荐
牛客小白月赛50
【时间复杂度和空间复杂度】
How to implement interface current limiting?
Which position does Anxin securities rank? Is it safe to open an account?
R language generalized linear model function GLM, GLM function to build logistic regression model, analyze whether the model is over discrete, and use the ratio of residual deviation and residual degr
[time complexity and space complexity]
Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl
基于 MATLAB的自然过渡配音处理方案探究
1 张量的简单使用
Redis的ACID
Natural language inference with attention and fine tuning Bert pytorch
Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)
(一)keras手写数字体识别并识别自己写的数字
Scala 基础 (二):变量和数据类型
Dialogue with the senior management of Chang'an Mazda, new products will be released in Q4, and space and intelligence will lead the Japanese system
【从删库到跑路】JDBC 完结篇(一天学完系列!!学完赶紧跑!)
Redis 迁移(操作流程建议)1
心情不好,我就这样写代码
【力扣刷题】二分查找:4. 寻找两个正序数组的中位数
Simple use of tensor