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;
    }
}
时间: 2024-08-05 11:13:21

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

angular项目中使用jQWidgets

Angular CLI with jQWidgets In this tutorial, we will show you how to use https://cli.angular.io/ along with the Angular Components by jQWidgets.Please, follow the step by step instructions below: npm install -g angular-cli ng new myProject cd myProje

BZOJ 3043: IncDec Sequence

3043: IncDec Sequence Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 578  Solved: 325[Submit][Status][Discuss] Description 给定一个长度为n的数列{a1,a2...an},每次可以选择一个区间[l,r],使这个区间内的数都加一或者都减一.问至少需要多少次操作才能使数列中的所有数都一样,并求出在保证最少次数的前提下,最终得到的数列有多少种. Input 第一行一个正整数n 接

Lintcode: Segment Tree Modify

For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in this node's interval. Implement a modify function with three parameter root, index and value to change the node's value with [start, end] = [index, index

Wiggle Subsequence

A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two

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

OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务 OpenVAS基础知识 OpenVAS(Open Vulnerability Assessment System)是开放式漏洞评估系统,其核心部分是一个服务器.该服务器包括一套网络漏洞测试程序,可以检测远程系统和应用程序中的安全问题.OpenVAS不同与传统的漏洞扫描软件.所有的OpenVAS软件都是免费的,而且还采用了Nessus(一款强大的网络扫描工具)较早版本的一些开放插件.虽然Nessus很强大,但是该工具

RavenDb 报错 has already produced 16 map results for a source document 解决方案

今天发现 RAVenDb 数据库报告错误 Index 'OrderTicketIssueReportIndex' has already produced 16 map results for a source document 'ff0ff6ed-2eab-4fba-8a61-a1b85d9e14cb', while the allowed max number of outputs is 15 per one document. Please verify this index defini

使用SpringBoot校验客户端传来的数据

前端的数据校验都是辣鸡!后端天下第一! 很多时候我们后端需要前端传数据过来, 比如注册, 修改用户名, 修改密码等等.很可能有些用户就喜欢搞事, 喜欢发一大堆乱七八糟的数据到后端来, 甚至有些前端老哥甚至都不做校验, 简直气死人.所以我们后端必须自己做校验.这节介绍一下如何优雅地做数据校验. 做数据校验还是有原则的, 只有一条:不要相信前端传过来的任何数据. 如果想完美地贯彻原则, 理论上来说就应该让前端那边少传数据过来, 有些的东西能查的就自己查出来. 常用的数据校验like this: /*

Reading and Modifying Asset File Paths in the 3ds Max File

Some people found the following information in the MAXScript reference for 2010: NEW in 3ds Max 2010: The 3ds Max scene file now provides Asset Metadata in a separate stream which can be accessed and modified by external applications. The Asset data

HDU 1081 To The Max

题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in th