当前位置:网站首页>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.
边栏推荐
- Data center concept
- Map of mL: Based on Boston house price regression prediction data set, an interpretable case is realized by using the map value to the LIR linear regression model
- Detailed index of MySQL
- nowcoder重排链表
- Nowcoder rearrange linked list
- Explain of SQL optimization
- 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
- 商業智能BI財務分析,狹義的財務分析和廣義的財務分析有何不同?
- Wt588f02b-8s (c006_03) single chip voice IC scheme enables smart doorbell design to reduce cost and increase efficiency
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
猜你喜欢

Alcohol driving monitoring system based on stm32+ Huawei cloud IOT design

实战解惑 | OpenCV中如何提取不规则ROI区域

A keepalived high availability accident made me learn it again

Combined with case: the usage of the lowest API (processfunction) in Flink framework

Data warehouse interview question preparation

RK1126平台OSD的实现支持颜色半透明度多通道支持中文

潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)

Digi重启XBee-Pro S2C生产,有些差别需要注意

Test process arrangement (3)

nowcoder重排链表
随机推荐
Wt588f02b-8s (c006_03) single chip voice IC scheme enables smart doorbell design to reduce cost and increase efficiency
92. (cesium chapter) cesium building layering
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
Leetcode T47: 全排列II
商业智能BI财务分析,狭义的财务分析和广义的财务分析有何不同?
Popular framework: the use of glide
Test process arrangement (3)
MySQL的触发器
尊重他人的行为
C language programming
sql优化之explain
AI and Life Sciences
Abnormal value detection using shap value
AI与生命科学
sql优化之查询优化器
LVGL 8.2 Line
Nowcoder rearrange linked list
Explain of SQL optimization
Some problems and ideas of data embedding point
Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)