- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
Given an array of integers, find a maximum sum of non-adjacent elements. For example, inputs [1, 0, 3, 9, 2,-1] should return 10 (1 + 9).
应该避免使用 3,2,因为 9 与 3,2 相邻。 maximum in array + maximum in Non adjacent elements of 9(array maximum element in array)
因为最大元素是 9 并且下一个最大值应该是不相邻的。得到这个 9+1=10(因为 1 是最大值的非相邻元素中的最大值)。
我在 O(n)+O(Max_index-1)+O(Array.length-Max_index+2) 中尝试过这种方式。
有没有其他方法可以在时间复杂度方面优化这段代码。
import java.io.*;
import java.util.*;
//Maximum Sum of Non-adjacent Elements
public class Test{
public static void main(String args[])
{
int[] a={1, 0, 3, 9, 2,-1,-2,-7};
int max=a[0];
int max_index=0;
for(int i=1;i<a.length;++i)
{
if(max<a[i])
{
max=a[i];
max_index=i;
}
}
int m1=a[0];
for(int i=1;i<max_index-1;++i) //get maximum in first half from 0 to max_index-1
{
if(m1<a[i])
m1=a[i];
}
int m2=a[max_index+2];
for(int i=max_index+2;i<a.length;i++)//get maximum in second half max_index+2 to end in array.
{
if(a[i]>m2)
m2=a[i];
}
int two_non_adj_max=max+Math.max(m1,m2);
System.out.println(two_non_adj_max);
}
}
最佳答案
您在线性时间内搜索最大值 M1。
你在 linesr 时间内搜索第二个不相邻的最大值 M2。
S1 = M1 + M2
如果 M1 是第一个或最后一个元素,则答案是 S1。
否则,您将与 M1 相邻的两个值相加:
S2 = A1 + A2
那么解就是max(S1, S2)
好的,ShreePool 对 S1 具体感兴趣。对于可能感兴趣的其他人来说,唯一可能具有更大和的非相邻元素对恰好是 A1 和 A2,就好像其中一个不相邻一样,它不会与 M1 相邻并且它会已成为 S1 的候选人。
现在,要在线性时间内找到 M1 和 M2,有几种选择。我写了一个只需要通过一次的。
Precondition: size >= 3;
function nonAdjacentMaxPair(a: Integer [], size: Integer): Integer [] is
var first: Integer;
var second: Integer;
var third: Integer;
var maxs: Integer [2];
var i: Integer;
first := 0;
second := 1;
third := 2;
if (A [1] > A [0]) then
first := 1;
second := 0;
endif;
if (A [2] > A [1]) then
third := second;
second := 2;
if (A [2] > A [0]) then
second := first;
first := 2;
endif;
endif;
i := 3;
while (i < size) do
if (A [i] > A [third]) then
third := i;
if (A [i] > A [second]) then
third := second;
second := i;
if(A [i] > A [first]) then
second := first;
first := i;
endif;
endif;
endif;
i := i + 1;
endwhile;
maxs [0] := first;
maxs [1] := second;
if (second = first + 1 or second = first - 1) then
maxs [1] := third;
endif;
return maxs;
endfunction;
而S1是A[maxs[0]] + A[maxs[1]]
希望这就是您所需要的。
记录:A1 + A2 是 A [maxs [0] - 1] + A [maxs [0] + 1],如果 maxs [0] 既不是 0 也不是 size。
关于arrays - 一维数组中非相邻元素的最大总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40981184/
我想在数组中找到连接(相邻)的元素。 例如,在数组中: [1,2,3,4,5] 要访问所有 2 个连通元素,输出将为: 1,2 2,3 3,4 4,5 要访问所有 3 个连通元素,输出将为 1,2,3
我有三个 Sprite ,彼此堆叠在一起。 我修改了他们的 transform.matrix 以给出他们一致增长的外观。 但是,根据比例因子,瓷砖之间有时会出现小裂缝。 cracks between
我正在阅读有关 Margin Collapsing 的文章,我遇到了这个:margin Adjacent siblings The margins of adjacent siblings are c
float div 的框阴影被其右侧的邻居截断,但左侧未截断。 我玩过 z-index 和 overflow: visible,但没有用。 HTML: CSS: .doc-page {
我有多个元素说卡片。这些卡片需要水平堆叠并且高度需要相同。这正在发生在我身上。 每张卡片都有图像、文本和按钮。每张卡片的图像和文本应采用任何卡片中的最大高度,以便它们正确对齐。这不会发生在我身上。 如
我有这个 GUI 我使用了 GridBagLayout,但我不知道为什么 Plain Bread 复选框与其相应的标签之间有很大的间距。 而且,我尝试仅增加按钮沿 x 轴的间距,但尽管重置了插图,但沿
在过去,我已经为自定义元素符号使用了数百次列表项背景图像,但不知何故从未遇到过这个问题。 基本上,我有一个 IMG float 在无序列表的左侧。元素符号背景图像设置在每个 LI 的左上角。但是, f
我正在使用 Bootstrap 框架并使用 2 列网格。 html 内容有标题、链接、副标题和文本。这增加了该列的高度。我希望它旁边的列与其高度匹配(以便图像显示)没有设置高度图像不显
我有一个 php 代码可以生成数百个 和 标签。我的问题如下,我有以下内容: X X 我想要第二个的边框颜色更重要,以便共享边框显示为灰色而不是黑色。我可以在第二个 td 中使用重要性或继承标签吗?
Place holder for Radio1 Place holder for Radio2 在此,我只想要 与相应的单选按钮相关联是可见的,但是...... * { visibilit
我正在尝试在 html 中实现以下布局。更大的 div 1。然后是它旁边的另一个 div,顶部有一个边距。如果我给 float: left 给第一个 div,给第二个 div margin-top 也
假设我有 2 个名为 IN 和 MASK 的二进制输入。实际字段大小可能是 32 到 256 位,具体取决于用于完成任务的指令集。两个输入都会改变每次调用。 Inputs: IN = ...110
我想知道是否有一种简洁/一行的方法来执行以下操作: pack :: [a] -> [(a, a)] pack [] = [] pack [_] = [] pack (x:y:xs
下面的代码分为两部分,一部分处理头部的管理,另一部分处理“主体”,当我执行代码时引发下面的异常,我该如何解决该错误?我不知道下面的错误是什么原因造成的,错误是在react的解析上 错误: Line
http://imgur.com/a/DA5i4 在上面的两张图片中你可以看到我有一个主容器,里面装满了 3 个较小的 div,一个大的在中间,两个瘦的在两边,但是右边直到中间的 div 下面才开始。
正如我在标题中解释的所有内容,然后我将只为你们提供我现在拥有的代码,我一直在努力实现我想要的东西很长时间但没有运气......它表现得像响应式的,但即使调整大小我也想将其保持在原位...截图
我编写了一个 jquery 插件,它使用 Flot 将 HTML 表格转换为图表。 HTML 是从 XSLT 生成的,在 XSLT 中我有以下代码来调用我的插件。此代码尝试在 blah1 和 blah
我正在尝试实现这样的布局。 aaa xxxxxx oooo aaa xxxxxx oooo xxxxxx xxxxxx bbb xxxxxx cccc bbb xxxxxx cc
在包含网格的 2 个 div 上使用内联 css 显示不起作用 最佳答案 为您的 div 指定宽度并根据您的要求使用“float: left”或“right”。不要对 div 使用“内联” 例如 CS
我将 MVC 项目中的一些代码返回到网页。我无法用撇号解决问题,当我的电话看起来像这样时,我如何忽略它 document.getElementById('some').insertAdjacentHT
我是一名优秀的程序员,十分优秀!