【Codewars】Maximum subarray sum

17 篇文章 0 订阅
6 篇文章 0 订阅

Codewars里的 5kyu Kata。

题目说明:

The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers:

Max.sequence(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4});
// should be 6: {4, -1, 2, 1}

Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead.

Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray.

动态规划题目,总感觉在哪里见过?求的是连续最大和的值,如果不熟悉动态规划的话,可以尝试使用二维数组解题,也是一种不错的锻炼。

解题代码:

public class Max {
    public static int sequence(int[] arr) {
        int len = arr.length;
        if (len == 0)
            return 0;
        if (len == 1)
            return arr[0] > 0 ? arr[0] : 0;
        // int[] dp = new int[len];
        // dp[0] = arr[0] > 0 ? arr[0] : 0;
        int dp = arr[0] > 0 ? arr[0] : 0;
        int max = arr[0] > 0 ? arr[0] : 0;

        for (int i = 1; i < len; i++) {
            if (arr[i] > 0) {
                // dp[i] = arr[i] + dp[i - 1];
                dp += arr[i];
            } else {
                // max = max > dp[i - 1] ? max : dp[i -1];
                // dp[i] = dp[i - 1] + arr[i] > 0 ? dp[i - 1] + arr[i] : 0;
                max = max > dp ? max : dp;
                dp = dp + arr[i] > 0 ? dp + arr[i] : 0;
            }
        }
        // return max > dp[len - 1] ? max : dp[len - 1];
        return max = max > dp ? max : dp;
    }
}

Test Cases:

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class MaxSequenceTest {
    @Test
    public void testEmptyArray() throws Exception {
        assertEquals("Empty arrays should have a max of 0", 0, Max.sequence(new int[] {}));
    }

    @Test
    public void testExampleArray() throws Exception {
        assertEquals("Example array should have a max of 6", 6,
                Max.sequence(new int[] { -2, 1, -3, 4, -1, 2, 1, -5, 4 }));
    }

    @Test
    public void testNegativeExampleArray() throws Exception {
        assertEquals("Example array with all negative values should have a max of 0", 0,
                Max.sequence(new int[] { -2, -1, -3, -4, -1, -2, -1, -5, -4 }));
    }

    @Test
    public void testRandomArrays() throws Exception {
        for (int i = 0; i < 50; i++) {
            int[] rand = randArr((int) (Math.random() * 10 + 50));
            assertEquals("Random array solution not as expected: ", solution(rand), Max.sequence(rand));
        }
    }

    private int[] randArr(int size) {
        int[] rand = new int[size];
        for (int i = 0; i < size; i++)
            rand[i] = (int) (Math.random() * 60 - 30);
        return rand;
    }

    private int solution(int[] arr) {
        int m = 0, a = 0, s = 0;
        for (int e : arr) {
            s += e;
            m = Math.min(s, m);
            a = Math.max(a, s - m);
        }
        return a;
    }
}

个人总结:

动态规划数组被我替换成了一个值,节省了内存空间。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值