当前位置:网站首页>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;
}
}
边栏推荐
- Redis Guide (8): principle and implementation of Qianfan Jingfa distributed lock
- 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
- Stepn novice introduction and advanced
- Notes on key review of software engineering at the end of the term
- 基于 MATLAB的自然过渡配音处理方案探究
- Unlock the value of data fusion! Tencent angel powerfl won the "leading scientific and Technological Achievement Award" at the 2021 digital Expo
- 1-12vmware adds SSH function
- What is the difference between stm32f1 and gd32f1?
- 牛客编程题--必刷101之动态规划(一文彻底了解动态规划)
- Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system
猜你喜欢
IAR engineering adapts gd32 chip
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!
TCP congestion control details | 1 summary
SAP OData development tutorial - from getting started to improving (including segw, rap and CDP)
The first batch in the industry! Tencent cloud security and privacy computing products based on angel powerfl passed CFCA evaluation
"C language" question set of ⑩
2 three modeling methods
NFT contract basic knowledge explanation
Big talk Domain Driven Design -- presentation layer and others
Beijing University and Tencent jointly build angel4.0, and the self-developed in-depth learning framework "River map" is integrated into the ecology
随机推荐
Exquisite makeup has become the "soft power" of camping, and the sales of vipshop outdoor beauty and skin care products have surged
(一)keras手写数字体识别并识别自己写的数字
Mono 的一些实例方法
What is the process of switching C # read / write files from user mode to kernel mode?
5 model saving and loading
安信证券排名第几位?开户安全吗?
Data analysis - numpy quick start
10 tf. data
Practice of federal learning in Tencent micro vision advertising
振动式液量检测装置
数据分析----numpy快速入门
Redis顺序排序命令
This year, the AI score of college entrance examination English is 134. The research of Fudan Wuda alumni is interesting
LeetCode 单周赛298,前三题
100+数据科学面试问题和答案总结 - 基础知识和数据分析
首例猪心移植细节全面披露:患者体内发现人类疱疹病毒,死后心脏重量翻倍,心肌细胞纤维化丨团队最新论文...
Redis的ACID
【力扣刷题】单调栈:84. 柱状图中最大的矩形
《软件工程》期末重点复习笔记
How to create your own NFT (polygon) on opensea