当前位置:网站首页>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.
边栏推荐
- Chapter 17 process memory
- 毕业季-个人总结
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
- sql优化之explain
- 商業智能BI財務分析,狹義的財務分析和廣義的財務分析有何不同?
- LVGL 8.2 Sorting a List using up and down buttons
- 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
- sql优化之查询优化器
- MySQL triggers
- Classify boost libraries by function
猜你喜欢

Talk about 10 tips to ensure thread safety

LVGL 8.2 LED

聊聊保证线程安全的 10 个小技巧

Codeforce:c. sum of substrings

Data Lake (13): spark and iceberg integrate DDL operations

【信息检索】链接分析

STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于GPIO)

PyTorch的自动求导机制详细解析,PyTorch的核心魔法

Opencv learning notes - linear filtering: box filtering, mean filtering, Gaussian filtering

LVGL 8.2 text shadow
随机推荐
MySQL的存储过程练习题
《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
数据埋点的一些问题和想法
C language set operation
LVGL 8.2 Line wrap, recoloring and scrolling
Digi XBee 3 rf: 4 protocols, 3 packages, 10 major functions
Visual Studio调试方式详解
opencv3.2 和opencv2.4安装
sql优化之explain
程序员自曝接私活:10个月时间接了30多个单子,纯收入40万
MySQL的触发器
An overview of 2D human posture estimation
ML:SHAP值的简介、原理、使用方法、经典案例之详细攻略
[C language] Pointer written test questions
利用Shap值进行异常值检测
Free, easy-to-use, powerful lightweight note taking software evaluation: drafts, apple memo, flomo, keep, flowus, agenda, sidenote, workflow
Leetcode T48: rotating images
scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于GPIO)
MySQL stored procedure exercise