当前位置:网站首页>Maximum subarray cumulative sum less than or equal to K
Maximum subarray cumulative sum less than or equal to K
2022-07-27 03:58:00 【Xiao Lu wants to brush the questions】
List of articles
subject
Please return arr in , Find the cumulative sum of subarrays , yes <=K And is the largest
Returns the maximum cumulative sum
Their thinking

hypothesis 0~ 57 Total cumulative sum 1000, seek < =300 How to maximize the cumulative sum of
In fact, it is to find which prefix and is before >=700 And closest to 
1)0-0 See if there is >=700 And , No, , Prefixes and are 40
2)0-1 See if there is >=700 And , No, , Prefixes and are 100
3)0-2 See if there is >=700 And , No, , Prefixes and are 70
…
n)0-23 Prefixes and are 710
namely 24-57 The cumulative sum of is less than or equal to 300 Maximum cumulative sum
Store data in an ordered table , You can get away 700 Recent figures
Code
public static int getMaxLessOrEqualK(int[] arr, int K) {
// Record i Previous , The prefix and , Organize according to the order table
TreeSet<Integer> set = new TreeSet<Integer>();
// When there is no number , There is already a prefix and is 0 了
set.add(0);
int max = Integer.MIN_VALUE;
int sum = 0;
// Every step of i, All subarrays must be i At the end of the story , Find the cumulative sum of subarrays , yes <=K Of , And it's the biggest
for (int i = 0; i < arr.length; i++) {
sum += arr[i]; // sum -> arr[0..i];
if (set.ceiling(sum - K) != null) {
max = Math.max(max, sum - set.ceiling(sum - K));
}
set.add(sum); // The current prefix and add to set In the middle
}
return max;
}
边栏推荐
- 次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
- Programming implementation of eight queens
- Plato farm is expected to further expand its ecosystem through elephant swap
- Plato farm brings a new experience to community users through the LAAS protocol elephant swap
- Double disk: the main differences between DFS and BFS, the differences in ideology, and the differences in code implementation
- Mysql database related operations
- 路由策略第一关
- Contour detection based on OpenCV (1)
- unity之二维数组实现正六边形地图
- Data analysis and disassembly method of banyan tree in Bairong
猜你喜欢

connman介绍

04.在谷歌浏览器中安装模拟浏览器ChromeDriver的详细步骤

Deployment of ruoyi's environment and operation of the system

Smart pointer shared_ ptr、unique_ ptr、weak_ ptr

Introduction to redis

flask_ Reqparse parser inheritance in restful

Application, addition and deletion of B-tree
![Machine learning [Matplotlib]](/img/d1/31ec2f96ca96465c6863798c7db179.jpg)
Machine learning [Matplotlib]

九方智投是正规公司吗?一起聊聊九方智投

Cocos game practice-04-collision detection and NPC rendering
随机推荐
[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree
DataX无法连接对应的数据库(windows下可以,linux下失败)
Specific use of multithreading
Characteristics and determination scheme of Worthington pectinase
J-3-point practice in the second game of 2022 Niuke multi school
Csu18m91 is used as the master controller of the intelligent scale scheme
Reading notes of Kazuo Inamori's advice to young people
C language force deduction question 43 string multiplication. Optimized vertical
Six determination methods of Worthington peroxidase activity
数字孪生实际应用:智慧城市项目建设解决方案
Tool class of localdatetime sorted out by yourself
The job created by flinksqlclient will disappear after the restart of Flink. Is there any way?
Worthington papain dissociation system solution
Feitengtengrui d2000 won the "top ten hard core technologies" award of Digital China
Typescript TS basic knowledge interface, generics
NFT数字藏品系统开发:老牌文学杂志玩起新潮数字藏品
Debug mode in pycharm for detailed debugging
Redis spike case, learn from Shang Silicon Valley teacher in station B
Two help points distribution brings to merchants
Program to change the priority of the process in LabVIEW