interviewbit : Max Non Negative SubArrayBookmark Suggest Edit
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的更多相关文章
- 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 ...
- 11039 - Building designing
Building designing An architect wants to design a very high building. The building will consist o ...
- Lintcode: Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务
OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务 1. OpenVAS基础知识 OpenVAS(Open Vulnerability Assessment Sys ...
- Java类MemoryUsage查看虚拟机的使用情况
原文地址:https://www.cnblogs.com/xubiao/p/5465473.html Java类MemoryUsage,通过MemoryUsage可以查看Java 虚拟机的内存池的内存 ...
- angular项目中使用jQWidgets
Angular CLI with jQWidgets In this tutorial, we will show you how to use https://cli.angular.io/ alo ...
- BZOJ 3043: IncDec Sequence
3043: IncDec Sequence Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 578 Solved: 325[Submit][Statu ...
- kali本機安裝openvas的血淚史復盤
安裝openvas的血淚史 因爲學習的需要,需要裝openvas,但是在虛擬機裏面,無論怎麼更新跟新源,總是會有問題,一氣之下,便不用虛擬機了,將自己的物理機刷成了kali機,從此便進了一個大坑. 安 ...
- 从干将莫邪的故事说起--java比较操作注意要点
故事背景 <搜神记>: 楚干将.莫邪为楚王作剑,三年乃成.王怒,欲杀之.剑有雌雄.其妻重身当产.夫语妻曰:“吾为王作剑,三年乃成.王怒,往必杀我.汝若生子是男,大,告之曰:‘出户望南山,松 ...
随机推荐
- Linux下如何使CP命令不提示覆盖
在Linux下使用CP命令,经常会提示是否覆盖,如果是太批量的文件覆盖,老是这么提示,会很烦的.那如何解决这个问题呢? 我们先来看一下原因吧! 一般我们使用的命令是cp -rf sourcefile ...
- Swift function how to return nil
这两天在学习Stanford出品的iOS7的课程,这个课程去年也看过,但是看到第3课就不行了,满篇的OC,把人都搞晕了.这段时间因为要写个iOS的App,正好赶上了Swift问世,所以趁着这股劲继续学 ...
- [转]Linux Ubuntu上架设FTP
Linux Ubuntu上架设FTP http://www.blogjava.net/stonestyle/articles/369104.html 操作系统:ubuntu (GNU/Linux) 为 ...
- [读行者][学习LinqExpression和Reflection(Emit)]阅读TypeBuilderSample之ExampleFromTheArticle
前言 关于”读行者“ 俗语有云:"读万卷书,行万里路“.多读一些优秀代码,不仅可以锻炼我们读代码的能力(便于维护或相互交流),还可以吸取很多我们成长所需的知识点.多读,才能开阔我们的眼界,才 ...
- html表格属性
一.在表格中插入文字及图片 1.把图片及文字分开到不同的[tr]标签表格内. <html> <body> <table border="1" widt ...
- why does angular js rock
angularjs 入门教程 http://angular-tips.com/blog/2013/08/why-does-angular-dot-js-rock/ Practive the previ ...
- jquery,js引入css文件,js引入头尾
jquery,js引入css文件,js引入头尾 今天在项目中,需要把20多个页面加上头和尾部,头和尾是我写的,所以小师傅把这个工作交给我了. 我开始往里面加,先引入common.css,在body开始 ...
- php大文件下载
<?php header("Content-Type:text/html;charset:utf-8"); //class FileDownServer{ //$file_n ...
- 使用个推的时候出现Installation error: INSTALL_FAILED_DUPLICATE_PERMISSION
使用个推的时候出现 Installation error: INSTALL_FAILED_DUPLICATE_PERMISSION perm=getui.permissio... 解决办法: 先将手机 ...
- Angular 2 Quickstart
写一个Angular 2的应用最基本的步骤概括为三步:写root组件,启动它(Boostrap),写index.html. 一些关于命名空间的基本知识 把所有代码放入一个立即调用函数中,通过传入一个空 ...