Coins in a Line II

There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins.

Could you please decide the first player will win or lose?

Example
Given values array A = [1,2,2], return true.

Given A = [1,2,4], return false.

DP

public boolean firstWillWin(int[] values) {
    // write your code here
    int size = values.length;  
    if(2 >= size) return true;  
    int []f = new int[size];
    f[size-1] = values[size-1];  
    f[size-2] = values[size-2]+values[size-1];  

    for(int i = size-3; i >=0; --i)  
    {  
        f[i] = Math.max(values[i]-f[i+1], values[i]+values[i+1]-f[i+2]);  
    }  
    return f[0] > 0;  
}

results matching ""

    No results matching ""