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. require.js的用法

    我采用的是一个非常流行的库require.js. 一.为什么要用require.js? 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文 ...

  2. 调用webservice客户端方法 runtime modeler error: Wrapper class ××× is not found. Have you run APT to generate them?

    用wsimport生成webservice的客户端以后,调用客户端生成方法时总是出现 runtime modeler error: Wrapper class stardand.nrcms.nckin ...

  3. 如何使用css和jquery控制文章标题字数?

    如何使用css控制文章标题字数? 最佳答案 控制文章标题字数,不是动态网页的专利,如果静态页面使用CSS样式,也可以实现相同的效果! 看这个例子,你们可以复制到记事本保存为HTML文件看效果! < ...

  4. TF-IDF与余弦相似性的应用(二):找出相似文章

    上一次,我用TF-IDF算法自动提取关键词. 今天,我们再来研究另一个相关的问题.有些时候,除了找到关键词,我们还希望找到与原文章相似的其他文章.比如,"Google新闻"在主新闻 ...

  5. ELK kibana查询与过滤(17th)

    在kibana中,可通过搜索查询过滤事务或者在visualization界面点击元素过滤. 创建查询 在Discover界面的搜索栏输入要查询的字段.查询语法是基于Lucene的查询语法.允许布尔运算 ...

  6. 【Search Insert Position 】cpp

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

  7. shell ulimit -n

    通过ulimit -n命令可以查看linux系统里打开文件描述符的最大值,一般缺省值是1024,

  8. 【BZOJ】【3157】&【BZOJ】【3516】国王奇遇记

    数论 题解:http://www.cnblogs.com/zhuohan123/p/3726933.html copy一下推导过程: 令$$S_i=\sum_{k=1}^{n}k^im^k$$ 我们有 ...

  9. 【BZOJ】【1269】【AHOI2006】文本编辑器editor

    Splay Splay序列维护的模板题了……为了便于处理边界情况,我们可以先插入两个空格当作最左端和最右端,然后……其实本题主要考察的就是Build.splay和Findkth这三个操作,我们可以实现 ...

  10. js添加事件、移除事件、阻止冒泡、阻止浏览器默认行为等写法(兼容IE/FF/CHROME)

    转自:http://blog.csdn.net/itchiang/article/details/7769341 添加事件   var addEvent = function( obj, type, ...