当前位置:网站首页>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.
边栏推荐
- Nowcoder reverse linked list
- 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
- 开发中常见问题总结
- remount of the / superblock failed: Permission denied
- 一文概览2D人体姿态估计
- Digi XBee 3 rf: 4 protocols, 3 packages, 10 major functions
- 基于51单片机的超声波测距仪
- 聊聊保证线程安全的 10 个小技巧
- Compile oglpg-9th-edition source code with clion
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
猜你喜欢

为什么国产手机用户换下一部手机时,都选择了iPhone?

Scratch Castle Adventure Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022

A keepalived high availability accident made me learn it again

电商系统中红包活动设计

【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制

Query optimizer for SQL optimization

Docker compose public network deployment redis sentinel mode

第十七章 进程内存

Test process arrangement (3)

【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
随机推荐
Opencv learning notes - linear filtering: box filtering, mean filtering, Gaussian filtering
Detailed analysis of pytorch's automatic derivation mechanism, pytorch's core magic
Sqlserver functions, creation and use of stored procedures
Leetcode T48: rotating images
redis 日常笔记
LVLG 8.2 circular scrolling animation of a label
MySQL的触发器
The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
Leetcode T49: 字母异位词分组
Excel quickly merges multiple rows of data
STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于GPIO)
flink sql-client. SH tutorial
Visual Studio调试方式详解
Industrial Internet has greater development potential and more industry scenarios
Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
Digi重启XBee-Pro S2C生产,有些差别需要注意
AI与生命科学
The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
数据埋点的一些问题和想法
【信息检索】链接分析