从给定数组中查找最低总和
我有一个数字[3,4,5,1,2,3,1]
查找3 Pairs
sub sequence say sub []
sub [0]< sub [1]> sub [2]
,总和这3个元素并获取最低总和。
示例:
对于[3,4,5,1,2,3,1]
,我可以选择[1,2,1]
这里1< 2> 1
因此,总和是1+2+1 = 4
最小。
约束:
数组尺寸最高1,00,000 每个元素大小为1至1,00,00,00,000
我的方法是使用3个嵌套进行循环,并获得最小总和,这不是有效的方法。
public long process(List<Integer> list) {
int n = list.size();
long output = Long.MAX_VALUE;
for(int i=0; i<n; i++) {
for(int j=i+1; j<n; j++) {
if(list.get(i) < list.get(j)) {
for(int k=j+1; k<n; k++) {
if(list.get(j) > list.get(k)) {
output = Math.min(output, list.get(i)+list.get(j)+list.get(k));
}
}
}
}
}
return output;
}
如何以较小的时间复杂性有效地解决该程序?
I have an array of numbers [3,4,5,1,2,3,1]
find 3 pairs
sub sequence say sub[]
such that sub[0] < sub[1] > sub[2]
, sum those 3 elements and get the minimum sum.
Example:
For [3,4,5,1,2,3,1]
, I can select [1,2,1]
here 1<2>1
so sum is 1+2+1 = 4
which is minimum.
Constraints:
array size upto 1,00,000
each element size is 1 to 1,00,00,00,000
My approach is using 3 nested for loops and getting the minimum sum which is not an efficient way.
public long process(List<Integer> list) {
int n = list.size();
long output = Long.MAX_VALUE;
for(int i=0; i<n; i++) {
for(int j=i+1; j<n; j++) {
if(list.get(i) < list.get(j)) {
for(int k=j+1; k<n; k++) {
if(list.get(j) > list.get(k)) {
output = Math.min(output, list.get(i)+list.get(j)+list.get(k));
}
}
}
}
}
return output;
}
How do solve this program efficiently with less time complexity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我提供一个解决方案,其时间复杂性为
o(n)
,空间复杂性为o(n)
。您必须通过三次阵列迭代,并存储两个阵列以达到最低元素。我受到@SomeOne的评论的启发。请记住,该解决方案可以假设任何sub [i]&lt; sub [j]&gt; sub [k]
必须保持:i&lt; J&LT; k
。可以轻松修改解决方案以覆盖
i&lt; = j&lt; = k
的情况。如果该方程式没有强制性,那么问题就会变得更加琐碎。只需找到前三个最小元素,我们知道sub [i]&lt; sub [j]&gt; sub [k]
保持。确保第三个(最大)与其他一个不同。尽管您没有指定我上面提到的规则,但我相信问题要您遵守该规则 - 否则这将非常微不足道。输出:
Let me provide a solution whose time complexity is
O(n)
and space complexity isO(n)
. You have to iterate through the array thrice and also store two arrays for the minimum elements. I was inspired by the comment made by @Someone. Please keep in mind that this solution makes the assumption that for anysub[i] < sub[j] > sub[k]
this must hold:i < j < k
.Solution can be modified easily to cover the cases where
i <= j <= k
. If it's not compulsory for this equation to hold, then question becomes more trivial. Just find first three minimum element and we know thatsub[i] < sub[j] > sub[k]
holds. Make sure that the third one (largest one) is different than the others. Although you didn't specify the rule I mentioned above, I believe question wants you to comply with that rule - otherwise that would be very trivial.Output:
相同的代码和添加的次要约束以考虑角案例:
The same code with minor constraints added to consider corner cases: