当前位置:网站首页>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;
}
}
边栏推荐
- 2 三种建模方式
- Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl
- 用Attention和微调BERT进行自然语言推断-PyTorch
- Comprehensive analysis of discord security issues
- 神经网络“炼丹炉”内部构造长啥样?牛津大学博士小姐姐用论文解读
- JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
- 油田勘探问题
- 01 backpack DP
- 2 three modeling methods
- 3. Keras version model training
猜你喜欢

【力扣刷题】11.盛最多水的容器//42.接雨水

Supplement the short board - Open Source im project openim about initialization / login / friend interface document introduction

首例猪心移植细节全面披露:患者体内发现人类疱疹病毒,死后心脏重量翻倍,心肌细胞纤维化丨团队最新论文...

Make up the weakness - Open Source im project openim about initialization / login / friend interface document introduction

SAP OData development tutorial - from getting started to improving (including segw, rap and CDP)

Exquisite makeup has become the "soft power" of camping, and the sales of vipshop outdoor beauty and skin care products have surged

NFT contract basic knowledge explanation

今年高考英语AI得分134,复旦武大校友这项研究有点意思

JS教程之使用 ElectronJS、VueJS、SQLite 和 Sequelize ORM 从 A 到 Z 创建多对多 CRUD 应用程序
![[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
随机推荐
Develop operator based on kubebuilder (for getting started)
[from deleting the database to running] the end of MySQL Foundation (the first step is to run.)
Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system
最小二乘系统辨识课 中篇:递归最小二乘
今年高考英语AI得分134,复旦武大校友这项研究有点意思
Anaconda3安装tensorflow 2.0版本cpu和gpu安装,Win10系统
mha 切换(操作流程建议)
神经网络“炼丹炉”内部构造长啥样?牛津大学博士小姐姐用论文解读
Solidus Labs欢迎香港前金融创新主管赵嘉丽担任战略顾问
This year, the AI score of college entrance examination English is 134. The research of Fudan Wuda alumni is interesting
TCP拥塞控制详解 | 1. 概述
Redis 迁移(操作流程建议)
[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
手写数字体识别,用保存的模型跑自己的图片
Common properties of XOR and addition
理想路径问题
Redis Guide (8): principle and implementation of Qianfan Jingfa distributed lock
Least squares system identification class II: recursive least squares
Learn about common functional interfaces
【从删库到跑路】MySQL基础 完结篇(入个门先跑路了。。)