Find out the maximum sub-array of non negative numbers from an array.
The sub-array should be continuous. That is, a sub-array created by choosing the second and fourth element and skipping the third element is invalid.

Maximum sub-array is defined in terms of the sum of the elements in the sub-array. Sub-array A is greater than sub-array B if sum(A) > sum(B).

Example:

A : [1, 2, 5, -7, 2, 3]
The two sub-arrays are [1, 2, 5] [2, 3].
The answer is [1, 2, 5] as its sum is larger than [2, 3]

NOTE: If there is a tie, then compare with segment's length and return segment which has maximum length
NOTE 2: If there is still a tie, then return the segment with minimum starting index

public class Solution {
public ArrayList<Integer> maxset(ArrayList<Integer> a) {
long maxSum = 0;
long newSum = 0;
ArrayList<Integer> maxArray = new ArrayList<Integer>();
ArrayList<Integer> newArray = new ArrayList<Integer>();
for (Integer i : a) {
if (i >= 0) {
newSum += i;
newArray.add(i);
} else {
newSum = 0;
newArray = new ArrayList<Integer>();
}
if ((maxSum < newSum) || ((maxSum == newSum) && (newArray.size() > maxArray.size()))) {
maxSum = newSum;
maxArray = newArray;
}
}
return maxArray;
}
}

interviewbit : Max Non Negative SubArrayBookmark Suggest Edit的更多相关文章

  1. interviewbit :Min Steps in Infinite GridBookmark Suggest Edit

    You are in an infinite 2D grid where you can move in any of the 8 directions : (x,y) to (x+1, y), (x ...

  2. 11039 - Building designing

      Building designing  An architect wants to design a very high building. The building will consist o ...

  3. Lintcode: Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  4. OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务

    OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务   1.  OpenVAS基础知识 OpenVAS(Open Vulnerability Assessment Sys ...

  5. Java类MemoryUsage查看虚拟机的使用情况

    原文地址:https://www.cnblogs.com/xubiao/p/5465473.html Java类MemoryUsage,通过MemoryUsage可以查看Java 虚拟机的内存池的内存 ...

  6. angular项目中使用jQWidgets

    Angular CLI with jQWidgets In this tutorial, we will show you how to use https://cli.angular.io/ alo ...

  7. BZOJ 3043: IncDec Sequence

    3043: IncDec Sequence Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 578  Solved: 325[Submit][Statu ...

  8. kali本機安裝openvas的血淚史復盤

    安裝openvas的血淚史 因爲學習的需要,需要裝openvas,但是在虛擬機裏面,無論怎麼更新跟新源,總是會有問題,一氣之下,便不用虛擬機了,將自己的物理機刷成了kali機,從此便進了一個大坑. 安 ...

  9. 从干将莫邪的故事说起--java比较操作注意要点

    故事背景 <搜神记>: 楚干将.莫邪为楚王作剑,三年乃成.王怒,欲杀之.剑有雌雄.其妻重身当产.夫语妻曰:“吾为王作剑,三年乃成.王怒,往必杀我.汝若生子是男,大,告之曰:‘出户望南山,松 ...

随机推荐

  1. 三星N8000/N8010通用刷机教程

    前面已经讲到过如何给三星n8000/n8010 Galaxy Note 10.1获取ROOT权限了.接下来就顺便告诉大家怎么给三星n8000/n8010刷机吧.其实给三星n8000/n8010刷机过程 ...

  2. MVC4.0 实现单一Action返回多种结果

    在开发过程中,我们往往会遇到这种情况.例如:展示学生的详细信息页面,加载学生的详细信息局部视图,异步请求学生的详细信息Json数据等等. 一般情况下,我们会写三个不同的action来支撑前台数据的调用 ...

  3. .Net开源数据库设计工具Mr.E For Linq (EF 6.1) 教程(三)更新已发布的数据库

    项目发布到服务器后,如果在后期,数据库的结构发生变更,如何更新到服务器呢? 首先,右键点击数据库,导出结构脚本文件 把脚本文件和 Mr.E.rar拷贝到服务器,在服务器解压Mr.E,运行其中的“更新数 ...

  4. 67.ARP协议

    ARP是地址解析协议Address Resolution Protocol的缩写.是一个位于TCP/IP协议栈中的低层协议,负责将某个IP地址解析程对应的MAC地址.在局域网中,网络实际传输的是“帧” ...

  5. QtSpim实现MIPS指令的编写

    QtSpim实现MIPS指令的编写 由于各种对齐问题,cnblogs的格式难以控制,故贴图片,谅解.

  6. Hibernate从入门到精通(十一)多对多双向关联映射

    上次我们在中Hibernate从入门到精通(十)多对多单向关联映射讲解了一下多对多单向关联映射,这次我们讲解一下七种映射中的最后一种多对多双向关联映射. 多对多双向关联映射 按照我们之前的惯例,先看一 ...

  7. C++中的运算符重载注意事项

    1.C++中的运算符重载的方式有三种: a.类成员函数重载 b.友元函数重载 c.普通函数重载 注意: a.我们主要使用的方式主要是用:类成员函数和友元函数来实现运算符的重载. b.其实用普通函数理论 ...

  8. cocos中常用到的单例模式

    单例:即只有一个类对象,且提供全局的访问权限 特点: 1.构造函数私有 2.私有的静态成员指针,标识是否已产生了单例实例 3.提供一个getInstance()方法来获取单例对象 下面已打飞机中的子弹 ...

  9. Mac下安装及配置Eclipse

    1.安装Eclipse前先确认你的Mac上是否已安装java运行环境.进入终端,输入”java -version”,如果返回了java版本号则说明已安装,否则,请先安装java运行环境: 2.访问ec ...

  10. 自动回复消息-微信公众平台开发4(asp.net)

    接着上一节的processRequest 处理函数,代码如下: /// <summary>    /// 处理微信发来的请求     /// </summary>    /// ...