查找给定 ArrayList 中总和最大的子数组
问题描述:
给定一个 Integers
的 ArrayList
。查找具有 ArrayList
中任何潜在子数组的最大总和的子数组。
子数组 a 是连续数字的组合。
子数组可以是任意长度n
,其中n >= 0
的大小。
示例
输入:
[-1, 10, -11, -1, 17, 0, 0, 9, 20, 7, -8, -6, -18]
解决方案
[17, 0, 0, 9, 20, 0, 7]
这是我到目前为止的代码。
public class MaxSubArray {
public ArrayList<Integer> solution(ArrayList<Integer> nums) {
int maxSubArrSum = Integer.MIN_VALUE;
int greatest = Integer.MAX_VALUE;
int smallest = 0;
int start;
int end;
ArrayList<Integer> maxSubArr;
ArrayList<ArrayList<Integer>> lists = new ArrayList();
try {
for (int left = 0; left < nums.size(); left++) {
int runningSum = 0;
for (int right = left; right < nums.size(); right++) {
runningSum += nums.get(right);
if (runningSum >= maxSubArrSum) {
ArrayList<Integer> temp = new ArrayList<>();
maxSubArrSum = runningSum;
start = left;
end = right;
for (int i = start; i <= end; i++) {
temp.add(nums.get(i));
}
lists.add(temp);
}
}
}
for (int i = 0; i < lists.size(); i++) {
if (lists.get(i).size() < greatest) {
greatest = lists.get(i).size();
smallest = i;
}
}
maxSubArr = lists.get(smallest);
return maxSubArr;
} catch (Exception e) {
e.printStackTrace();
return nums;
}
}
}
我正在尝试迭代 nums
ArrayList
并找出 的 first 和 last 索引子数组与最大总和并将它们放入ArrayList
列表中。
之后,我试图找出哪个子数组具有最小的大小并返回它。
我在这里做错了什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是 Kadane 算法的修改版本,用于查找列表中连续元素的最大总和。它改编自 Python 中给出的解决方案,并且可以一次性运行。
印刷
Here is a modified version of Kadane's Algorithm to find the largest sum of contiguous elements in a list. It is adapted from a solution given in Python and works in a single pass.
prints
你的接近很安静。
最后一部分有两个问题:
int Great = Integer.MAX_VALUE;
应该改为Integer.MIN_VALUE
。如果将最后一部分更改为:
通过利用
它会产生所需的结果。
You are quiet close with your approach.
There are two problems with the last part:
int greatest = Integer.MAX_VALUE;
should beInteger.MIN_VALUE
instead.if you change the last part to:
by utilizing
it yields the desired result.
基本上,您尝试使用暴力算法来完成此任务,在最坏的情况下,该算法的时间和空间复杂度将是O(n^2) 。
它可以通过时间和空间复杂度线性(即O(n))来完成,而无需嵌套循环。
使用这种方法,首先,我们需要使用 最大可能和 rel="nofollow noreferrer">Kadane 算法。
然后在单个循环中对源列表执行迭代,跟踪当前总和。当它等于最大和时,就意味着找到了连续元素的目标子数组。
变量
start
和end
表示结果子数组的起始和结束索引。方法
subList()
在源列表上创建一个视图,并且对视图的每次修改都将反映在源中,反之亦然。因此,作为预防措施,它被包裹在一个新的 ArrayList 实例中。Kadane 的算法实现。
总体思路是维护两个变量,分别表示全局和局部最大值。每次迭代时局部最大值都会发生变化,我们要么将
在每次迭代结束时,会将全局最大值与局部最大值进行比较,并根据需要进行调整。
main()
输出
Basically, you are trying to approach this task with a brute-force algorithm, which in the worse case scenario will have the O(n^2) both time and space complexity.
It could be done with a linear (i.e. O(n)) both time and space complexity, without nested loops.
With this approach, first, we need to find the maximum possible sum of the subarray by using the Kadane's algorithm.
And then perform the iteration with over the source list in a single loop tracking the current sum. When it becomes equal to the maximum sum it would mean the target subarray of consecutive elements was found.
Variables
start
andend
denote the starting and ending indices of the resulting subarray.Method
subList()
creates a view over the source list and every modification of the view will be reflected in the source and vice versa. Hence, as a precaution it's being wrapped with with a new instance ofArrayList
.Kadane's algorithm implementation.
The overall idea is to maintain two variables denoting the global and a local maximum. There are to ways in which the local maximum changes with each iteration, we either
At the end of every iteration, the global maximum is being compared with a local maximum and adjusted if needed.
main()
output
这是一个更简洁的解决方案
Here is a more concise solution
添加第三个内部 for 循环可以使任务变得更容易。想想你会如何用笔和纸来做这件事。假设您有一个包含 6 个元素的数组,其索引从
0
到5
,那么所有可能的子数组都将具有以下开始和结束索引(包含 strat,不包含 end)最重要的是,您需要计算总和并存储相关的开始和结束索引
Adding a third inner for-loop can make the task probably easier. Just think about how you would do it with a pen and paper. Imagine you have an array of 6 elements with indices from
0
to5
, then all possible subarrays would have the following start and end indices (strat inclusive, end exclusive)Having the above all you need is to calculate the subsum and store the relevant start and end indices