简体   繁体   English

非递减序列的最大和

[英]Largest sum of non-decreasing up sequence

eg. 例如。 -3,6,11,13,5,-11,4,7,8 largest sum =30 (6,11,13 cause adding -3 will make the sum smaller) -3,6,11,13,5,-11,4,7,8最大和= 30(6,11,13因为加-3会使和更小)

eg. 例如。 7,0,-3 largest sum = 7 7,0,-3最大和= 7

eg 4,-1,45 largest sum = 45 例如4,-1,45最大和= 45

eg -3-,-2,-6,0 largest sum =0 例如-3-,-2,-6,0最大和= = 0

Need some advise for my code, still buggy 需要一些建议给我的代码,仍然有错误

    int maxSum = Integer.MIN_VALUE;
    int sum = 0;
    int checkNeg = Integer.MIN_VALUE;

    for (int i = 0; i < a.length; i++) {
        if (a[i] > checkNeg) {
            checkNeg = a[i];
        }
    }

    if (checkNeg <= 0) {
        maxSum = checkNeg;
    }

    if (checkNeg > 0) {
        for (int i = 0; i < a.length; i++) {
            if (a[i] != 0) {
                sum += a[i];
                if (sum > maxSum) {
                    if (i != 0) {
                        if (a[i] >= a[i - 1]) {
                            maxSum = sum;
                        } else {
                            sum = a[i];
                        }
                    } else {
                        maxSum = sum;
                    }
                }
                if (sum < 0) {
                    sum = 0;
                }
            } else {
                sum = 0;
            }
        }
    }
    return maxSum;

Try this 尝试这个

        int maxSum = Int32.MinValue;
        int sum = 0;

            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] >= 0)
                {
                    sum += a[i];
                    maxSum = Math.Max(sum, maxSum);

                    if ((i+1<a.Length) && (a[i+1] < a[i]))
                        sum = 0;
                }
                else
                {
                    maxSum = Math.Max(a[i], maxSum);
                    sum = 0;
                }
            }

        return maxSum;

seemed to work for all your examples and a couple of others. 似乎适用于您的所有示例以及其他几个示例。

There is no need for 'checkNeg', this check should come out of the algorithm a little more naturally 不需要'checkNeg',该检查应该更自然地从算法中得出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 处理非递减的色彩亮度序列 - Working on a Non-decreasing Color Brightness Sequence 找到一个非递减的连续子数组 - Finding a non-decreasing contiguous subarray 数组中最长的非递减段的运行时间 - running time of longest non-decreasing segment in an array 以下用于更改数组以使其不减少的解决方案有什么问题? - What's wrong with the following solution for changing an array so that it becomes non-decreasing? 中值查询:- 长度为 A 的子数组,您必须按非递减顺序对子数组的元素进行排序 - Median Queries:- a subarray of A of length , you have to sort the elements of the subarray in a non-decreasing order 我想从斐波那契数列中减去最大可能的数字 - I want to subtract the largest possible number from the Fibonacci sequence by decreasing it in 给定一个按非递减顺序排序的 integer 数组 nums,就地删除重复项,使每个唯一元素只出现一次 - Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once 找到最长的递减顺序 - Find the longest decreasing sequence 确定Java中序列是否在减少 - Determine whether or not a sequence is decreasing in java 依次获得最大和第二大数字 - Getting largest and second largest number in a sequence
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM