当前位置:网站首页>Yyds dry goods inventory # solve the real problem of famous enterprises: continuous maximum sum
Yyds dry goods inventory # solve the real problem of famous enterprises: continuous maximum sum
2022-07-04 14:36:00 【51CTO】
1. sketch :
describe
An array has N Elements , Find the maximum sum of successive subarrays . for example :[-1,2,1], And the largest continuous subarray is [2,1], The sum is 3
Input description :
Enter in two lines . The first line is an integer n(1 <= n <= 100000), Means that there are n Elements Second behavior n Number , That is, every element , Every integer is in 32 position int Within the scope of . Space off .
Output description :
The largest value in all successive subarrays .
Example 1
Input :
Output :
2. Code implementation :
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sums=0, maxsums=Integer.MIN_VALUE; // Consider the case of all negative numbers
for(int i=0;i<n;i++){
sums+=sc.nextInt();
maxsums=Math.max(maxsums,sums);
sums= sums<0?0:sums; // Code core , If the current sum is negative , Then discard the previous continuous array , Start summing again
}
System.out.println(maxsums);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
边栏推荐
- 關於miui12.5 紅米k20pro用au或者povo2出現問題的解决辦法
- [cloud native] how can I compete with this database?
- 【算法leetcode】面试题 04.03. 特定深度节点链表(多语言实现)
- LVLG 8.2 circular scrolling animation of a label
- Wt588f02b-8s (c006_03) single chip voice IC scheme enables smart doorbell design to reduce cost and increase efficiency
- 利用Shap值进行异常值检测
- C language small commodity management system
- 开发中常见问题总结
- Test evaluation of software testing
- 实时数据仓库
猜你喜欢
Leetcode 61: 旋转链表
Test process arrangement (2)
Ultrasonic distance meter based on 51 single chip microcomputer
潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
【信息检索】链接分析
Test evaluation of software testing
Digi XBee 3 RF: 4个协议,3种封装,10个大功能
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
阿里被裁员工,找工作第N天,猎头又传来噩耗...
NowCoder 反转链表
随机推荐
ML:SHAP值的简介、原理、使用方法、经典案例之详细攻略
R language uses follow up of epidisplay package The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The col parameter specifies the
Respect others' behavior
[cloud native] how can I compete with this database?
曝光一下阿里的工资待遇和职位级别
毕业季-个人总结
R language dplyr package summary_ If function calculates the mean and median of all numerical data columns in dataframe data, and summarizes all numerical variables based on conditions
LVGL 8.2 text shadow
First experience of ViewModel
Learn kernel 3: use GDB to track the kernel call chain
Gin integrated Alipay payment
scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
Detailed explanation of visual studio debugging methods
Test process arrangement (3)
Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on GPIO)
MySQL stored procedure exercise
炒股网上开户安全吗?会不会被骗。
Node mongodb installation
深度学习7 Transformer系列实例分割Mask2Former
R language uses the mutation function of dplyr package to standardize the specified data column (using mean function and SD function), and calculates the grouping mean of the standardized target varia