当前位置:网站首页>力扣解法汇总1413-逐步求和得到正数的最小值
力扣解法汇总1413-逐步求和得到正数的最小值
2022-08-09 12:50:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
原题链接:
描述:
给你一个整数数组 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
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-value-to-get-positive-step-by-step-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路:
* 这题转换一下思路,其实最要求遍历累加过程中的最小值就可以了,
* 求出这个最小值min后,
* 如果min<0时,startValue= 1- min
* min>0时,startValue=0
代码:
public class Solution1413 {
public int minStartValue(int[] nums) {
int min = 0;
int sum = 0;
for (int i : nums) {
sum += i;
min = Math.min(min, sum);
}
if (min == 0) {
return 1;
}
return 1 - min;
}
}
边栏推荐
- ansible-cmdb friendly display ansible collects host information
- glibc 内存管理模型 释放 C库内存缓存
- 技术分享 | 接口自动化测试如何处理 Header cookie
- Standing wave ratio calculation method
- ctfshow七夕杯2022
- 剑指 Offer 56 - II. 数组中数字出现的次数 II(位运算)
- 二维数组&指针
- LeetCode 37.解数独
- Flutter introduction advanced trip (5) Image Widget
- Customize VIEW to realize in-app message reminder to rotate up and down
猜你喜欢
Do you know the difference between comments, keywords, and identifiers?
kustomize入门示例及基本语法使用说明
jenkins api create custom pipeline
Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
Flutter Getting Started and Advanced Tour (3) Text Widgets
快来扔鸡蛋。
The FPGA - work summary recently
5G China unicom repeater network management protocol real-time requirements
5G China unicom AP:B SMS ASCII 转码要求
随机推荐
Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
5G China unicom general exception handling
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
JVM内存泄漏和内存溢出的原因
FPGA-在ISE中错误总结(更新中)
2022年非一线IT行业就业前景?
5G China unicom AP:B SMS ASCII 转码要求
某高校的R语言数据分析期末作业
昇腾AI开发者创享日南京站!一起CANN机器狗+AI机械臂实现硬核智慧救援!燃爆现场~
注:检测到当前使用的ADB不是HBuilder内置或自定义ADB:PID为:9544进程名称为:adb.exe 路径为:c:\users\administrator\appdata\local\and
kustomize入门示例及基本语法使用说明
NFS pays special attention to the problem of permissions
The new features of ABP 6.0.0 - rc. 1
陈强教授《机器学习及R应用》课程 第十三章作业
GIN中GET POST PUT DELETE请求
ftplib+ tqdm upload and download progress bar
Clock frequency and baud rate count for serial communication in FPGA
CPU-MIPS32 instruction architecture (unlocked pipeline microprocessor)
CPU-MIPS32指令架构(无内锁流水线微处理器)
剑指 Offer 56 - II. 数组中数字出现的次数 II(位运算)