动态编程(Codility Q:NumberSolitaire)

时间:2015-12-05 01:52:58

标签: java dynamic-programming

这是一个问题: codility.com/programmers/task/number_solitaire

以下链接是我的结果(50%来自Codility): https://codility.com/demo/results/training8AMJZH-RTA/

我的代码(首先,我试图使用Kadane的Algo解决这个问题):

class Solution {
    public int solution(int[] A) {
        int temp_max = Integer.MIN_VALUE;
        int max = 0;
        int k = 1;

        if(A.length == 2) return A[0] + A[A.length-1];

        for(int i = 1; i < A.length-1; i++) {
            if(temp_max < A[i]) temp_max = A[i];

            if(A[i] > 0) {
                max += A[i];
                temp_max = Integer.MIN_VALUE;
                k = 0;           

            } else if(k % 6 == 0) {
                max += temp_max;
                temp_max = Integer.MIN_VALUE;
                k = 0;
            }
            k++;
        }
        return A[0] + max + A[A.length-1];
    }

以下是我在网上找到的解决方案(来自Codility结果的100%):

class Solution {
    public int solution(int[] A) {
        int[] store = new int[A.length];
        store[0] = A[0];
        for (int i = 1; i < A.length; i++) {
            store[i] = store[i-1];
            for (int minus = 2; minus <= 6; minus++) {
                if (i >= minus) {
                    store[i] = Math.max(store[i], store[i - minus]);
                } else {
                    break;
                }
            }
            store[i] += A[i];
        }
        return store[A.length - 1];
    }
}

我不知道我的代码有什么问题:(

我尝试了几个测试用例但是,与解决方案没有什么不同。我的代码

但是,痘痘测试结果显示我的不完全正确。 (https://codility.com/demo/results/training8AMJZH-RTA/

请有人用我的代码解释我的问题~~

8 个答案:

答案 0 :(得分:1)

因为您没有使用动态编程,所以您使用的是贪婪的算法。当范围内的最大数字不是正确的选择时,您的代码将失败。

&#13;
&#13;
{{1}}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

根据发布的解决方案,我编写了不错的可读代码。性能不是最好。

public struct PersonStruct
{
    public string Name { get; set; }
    public int Year { get; set; }
}

答案 2 :(得分:1)

来自Java的可读解决方案:

public class Solution {
    public static void main(String[] args) {
        System.out.println(new Solution().solution(new int[]{1, -2, 0, 9, -1, -2}));
    }

    private int solution(int[] A) {
        int N = A.length;
        int[] dp = new int[N];
        dp[0] = A[0];

        for (int i = 1; i < N; i++) {
            double sm = Double.NEGATIVE_INFINITY;
            for (int j = 1; j <= 6; j++) {
                if (i - j < 0) {
                    break;
                }
                double s1 = dp[i - j] + A[i];
                sm = Double.max(s1, sm);
            }
            dp[i] = (int) sm;
        }

        return dp[N-1];
    }
}

答案 3 :(得分:1)

这是一种类似于@ 0xAliHn的解决方案,但使用的内存更少。您只需要记住最近的6个动作即可。

def NumberSolitaire(A):
    dp = [0] * 6
    dp[-1] = A[0]

    for i in range(1, len(A)):
        maxVal = -100001

        for k in range(1, 7):
            if i-k >= 0:
                maxVal = max(maxVal, dp[-k] + A[i])

        dp.append(maxVal)
        dp.pop(0)
    return dp[-1]

答案 4 :(得分:0)

这是我的解决方案。我尝试使代码易于理解。它可能无法尽可能多地节省空间。

    private static int solution(int A[])
{   
    // N //  N is an integer within the range [2..100,000];
    // A[] // each element of array A is an integer within the range [−10,000..10,000].
    int N = A.length;
    int[] bestResult = new int[N]; // record the current bestResult
    Arrays.fill(bestResult, Integer.MIN_VALUE); // fill in with the smallest integer value

    // initialize
    bestResult[0] = A[0];
    for (int i = 0;i < A.length;i++) {
        // calculate six possible results every round
        for (int j = i + 1; (j < A.length) && (i < A.length) && j < (i + 1) + 6; j++) {
            // compare
            int preMaxResult = bestResult[j]; // the max number so far
            int nowMaxResult = bestResult[i] + A[j]; // the max number at bestResult[i] + A[j]
            bestResult[j] = Math.max(preMaxResult, nowMaxResult);
        }
    }        
    return bestResult[bestResult.length-1];
}

答案 5 :(得分:0)

以下是简单的Python 3解决方案:

import sys

def solution(A):        
    dp = [0] * len(A)
    dp[0] = A[0]

    for i in range(1, len(A)):
        maxVal = -sys.maxsize - 1

        for k in range(1, 7):
            if i-k >= 0:
                maxVal = max(maxVal, dp[i-k] + A[i])

        dp[i] = maxVal
    return dp[len(A)-1]

答案 6 :(得分:0)

100%c ++解决方案( results

#include <climits>

int solution(vector<int>& A) {
    const int N = A.size();
    if (N == 2)
        return A[0] + A[1];

    vector<int> MaxSum(N, INT_MIN);
    MaxSum[0] = A[0];
    for (int i = 1; i < N; i++) {
        for (int dice = 1; dice <= 6; dice++) {
            if (dice > i)
                break;
            MaxSum[i] = max(MaxSum[i], A[i] + MaxSum[i - dice]);
        }
    }
    return MaxSum[N-1];
}

答案 7 :(得分:0)

100% 蟒蛇解决方案 借助上述答案和https://sapy.medium.com/cracking-the-coding-interview-30eb419c4c57

def solution(A):
    # write your code in Python 3.6
    # initialize maxUntil [0]*n
    n = len(A)
    maxUntil = [0 for i in range(n)]
    maxUntil[0]=A[0]
    # fill in maxUntil, remember to chack limits
    for i in range(1, n): # for each 
        maxUntil[i] = maxUntil [i-1]
        # check the max 6 to the left:
        # for 1,..,6:
        for dice in range(1,7):
            if dice > i: # if dice bigger than loc - we are out of range
                break
            #else: check if bigger than cur elem, if so - update elem
            maxUntil[i] = max(maxUntil[i],maxUntil[i-dice])
        # add the current jump:
        maxUntil[i] +=A[i]
    # must reach the last sq:
    return maxUntil[n-1]