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比较操作注意要点
故事背景 <搜神记>: 楚干将.莫邪为楚王作剑,三年乃成.王怒,欲杀之.剑有雌雄.其妻重身当产.夫语妻曰:“吾为王作剑,三年乃成.王怒,往必杀我.汝若生子是男,大,告之曰:‘出户望南山,松 ...
随机推荐
- 检测到有潜在危险的 Request.Form 值。 说明: ASP.NET 在请求中检测到包含潜在危险的数据
在请求方法的顶部添加 [ValidateInput(false)]就OK了 从客户端(Content=" sdfdddd ...")中检测到有潜在危险的 Reques ...
- FTP上传文件夹
文件上传类 using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; usi ...
- 基础语法 swift
强类型语言:每句代码可以不用分号分隔:大小写敏感: 变量声明: var a = 0 常量声明 let b = 3.14 常量不能+变量?a+b 类型标注 var s :String 打印 pringl ...
- 关于sqlserver身份登录失败的解决方法
前几天写程序需要用到数据库,下载了一个用用,出现了不少的小问题(都怪我的32bit不争气的笔记本),有问题不要怕,至少证明我们在思考解决方案.废话不说了,直接上正题. Sqlserver有两种登陆方式 ...
- 字符串比较 忽略大小写 iphone
//不考虑大小写比较字符串1 NSString *astring01 = @"this is a String!"; NSString *astring02 = @"Th ...
- text-overflow 与 word-wrap:设置使用一个省略标记...标示对象内文本的溢出。
text-overflow 与 word-wrap text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 语法: 但是text-overflow只是用来说明文字溢出时 ...
- 问题:ldconfig
显示加载库文件libjli.so时候出错. 解决办法 1.find / -name 'libjli.so'文件 路径在:/data0/home/app/act/jdk/jdk1.7.0_15/jre/ ...
- Shell遍历文件的每一行[转载]
#!/bin/sh while read line do echo $line done < /home/jms/lab/input.txt
- 【Longest Valid Parentheses】cpp
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- 使用AzCopy跨账户迁移blob
昨天北美紧急通知要停掉几个开发和测试的订阅,当天必须完成,因为事情比较多,搞得有点我措手不及,但是唯一的遗憾是Azure VM. 因为在上面做了很多东西,很多资料和环境都是做好的,如果被删除掉实在可惜 ...