当前位置:网站首页>Yyds dry inventory solution sword finger offer: maximum sum of continuous subarrays (II)
Yyds dry inventory solution sword finger offer: maximum sum of continuous subarrays (II)
2022-06-28 23:12:00 【51CTO】
1. sketch :
describe
Enter a length of n Integer array array, One or more consecutive integers in an array form a subarray , Find a continuous subarray with the largest sum .1. Subarray is continuous , such as [1,3,5,7,9] The subarrays of are [1,3],[3,5,7] wait , however [1,3,7] It's not a subarray 2. If there are multiple consecutive subarrays with the largest sum , Then return the longest , The data of this question ensure that there is only one 3. The minimum length of the subarray defined by this question is 1, There is no empty subarray , That there is no [] Is a sub array of an array 4. The returned array is not included in the space complexity calculation
Data range :
requirement : Time complexity , Spatial complexity
Advanced : Time complexity
, Spatial complexity
Example 1
Input :
Return value :
explain :
Example 2
Input :
Return value :
Example 3
Input :
Return value :
explain :
Example 4
Input :
Return value :
explain :
2. Code implementation :
import java.util.*;
public class Solution {
public int[] FindGreatestSumOfSubArray (int[] array) {
int x = array[0];
int y = 0;
int maxsum = x;
// Sliding interval
int left = 0, right = 0;
// Record the longest interval
int resl = 0, resr = 0;
for(int i = 1; i < array.length; i++){
right++;
// State shift : Continuous subarray and maximum
y = Math.max(x + array[i], array[i]);
// New starting point of the interval
if(x + array[i] < array[i])
left = right;
// Update Max
if(y > maxsum || y == maxsum && (right - left + 1) > (resr - resl + 1)){
maxsum = y;
resl = left;
resr = right;
}
// to update x The state of
x = y;
}
// Take an array
int[] res = new int[resr - resl + 1];
for(int i = resl; i <= resr; i++)
res[i - resl] = array[i];
return res;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
边栏推荐
- 运维排查-使用hcache插件排查Buffer/cache占用过高
- 第三章 处理机调度练习
- Go language - reflect
- Panxiaoming, senior vice president of IC nansha|amd and President of Greater China: process, architecture and platform optimization break through the computing boundary
- 2022-06-28:以下golang代码输出什么?A:true;B:false;C:panic;D:编译失败。 package main import “fm
- Code example of hiredis
- Prometeus 2.36.0 新特性
- Langage C - analyse des mots
- When dialogfragment's onstop is completely invisible, call disass to exit the interface and report an error. Solution
- 复杂嵌套的对象池(4)——管理多类实例和多阶段实例的对象池
猜你喜欢

Chapter II Classic synchronous exercises

On the necessity and solution of building a campus online teaching video convergence platform
![LeetCode 324 擺動排序 II[排序 雙指針] HERODING的LeetCode之路](/img/41/b8ba8d771b7224eac1cc8c54fe9d29.png)
LeetCode 324 擺動排序 II[排序 雙指針] HERODING的LeetCode之路

keil工程,程序写多后,RTT不能打印

第三章 处理机调度练习

A password error occurred when docker downloaded the MySQL image to create a database link

Leetcode detailed explanation of stack type
![[word Tutorial Series Part 1] how to remove arrows in word tables](/img/c7/dc57002b0e9d433c4dfac15d53713d.png)
[word Tutorial Series Part 1] how to remove arrows in word tables

Chapter V virtual memory exercise

【深度学习】(2) Transformer 网络解析,代码复现,附Pytorch完整代码
随机推荐
笔记
在长投学堂开通证券账户是安全可靠的吗?
[stm32 HAL库] RTC和BKP驱动
Leetcode detailed explanation of stack type
Panxiaoming, senior vice president of IC nansha|amd and President of Greater China: process, architecture and platform optimization break through the computing boundary
flowable 边界定时器
Progress of dbnn experiment
自媒体行业内卷严重:企业自媒体应该何去何从
非科班!自学之路!
C語言-單詞分析解析
Wave picking of WMS warehouse management system module
第三章 处理机调度练习
【Flutter 问题系列第 71 篇】Flutter 中 Uint8List 和 Image 之间的相互转换
The Best of Many Worlds_ Dual Mirror Descent for Online Allocation Problems
frameworks/base/core/res/res/values/symbols.xml:3915: error: no definition for declared symbol解决办法
Mono's execution process
LeetCode 324 擺動排序 II[排序 雙指針] HERODING的LeetCode之路
Prometeus 2.36.0 new features
Lock4j -- distributed locking Middleware -- use / instance
[matlab] function definition and use