当前位置:网站首页>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;
}
边栏推荐
- ZJCTF_ login
- 数字孪生应用及意义对电力的主要作用,概念价值。
- 768. Block II greed that can complete sorting at most
- 次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
- 04. Detailed steps for installing the simulated browser chromedriver in Google browser
- Will this flinkcdc monitor all tables in the database? Or the designated table? I look at the background log. It monitors all tables. If it monitors
- Machine learning [Matplotlib]
- Interview question: the difference between three instantiated objects in string class
- 一文读懂 | 数据中台如何支撑企业数字化经营
- Leetcode- > 2-point search and clock in (3)
猜你喜欢

Learning and understanding of four special data types of redis

If the detailed explanation is generated according to the frame code

GetObject call timing of factorybean

The fifth strong network cup national network security challenge Title reappearance (with title attachment, detailed explanation)
![[Android synopsis] kotlin multithreaded programming (I)](/img/04/4349bacbd401868d73a3b05d018b66.png)
[Android synopsis] kotlin multithreaded programming (I)

NFT数字藏品系统开发:老牌文学杂志玩起新潮数字藏品

Record the problem of PHP program accessing system files incorrectly

Deployment of ruoyi's environment and operation of the system

On the first day of Shenzhen furniture exhibition, the three highlights of Jin Ke'er booth were unlocked!

Program to change the priority of the process in LabVIEW
随机推荐
Two help points distribution brings to merchants
MySQL Chinese failure
Smart pointer shared_ ptr、unique_ ptr、weak_ ptr
C语言力扣第43题之字符串相乘。优化竖式
222. 完全二叉树的节点个数
Message queue learning -- Concepts
Director of meta quest content ecology talks about the original intention of APP lab design
回归测试:意义、挑战、最佳实践和工具
Solution to Chinese garbled code in console header after idea connects to database to query data
Design method and test method of APP interface use case
Data analysis and disassembly method of banyan tree in Bairong
一维数组的应用
If the detailed explanation is generated according to the frame code
LeetCode 第二十八天
Ming min investment Qiu Huiming: behind the long-term excellence and excess, the test is the team's investment and research ability and the integrity of strategy
NFT数字藏品系统开发:小蚁数智帮助品牌一键上链发行NFT
[Android synopsis] kotlin multithreaded programming (I)
C language force deduction question 43 string multiplication. Optimized vertical
Detailed tutorial of typera
J-3-point practice in the second game of 2022 Niuke multi school