【Codewars】Maximum subarray sum

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;
    }
}

個人總結:

動態規劃數組被我替換成了一個值,節省了內存空間。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章