zoukankan      html  css  js  c++  java
  • LintCode Coins in a Line III

    原题链接在这里:http://www.lintcode.com/en/problem/coins-in-a-line-iii/

    题目:

    There are n coins in a line. Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins.

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

    Example

    Given array A = [3,2,2], return true.

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

    Given array A = [1,20,4], return false.

    题解:

    类似Coins in a Line II.

    DP问题. 状态dp[l][r]第i到第j的硬币时,先手去硬币的人最后最多去硬币价值.

    转移方程, pickLeft = min(dp[l+2][r], dp[l+1][r-1])+values[l]

         pickRight = min(dp[l][r-2], dp[l-1][r-1])+values[r]

         dp[l][r] = max(pickLeft, pickRight)

    初始化l == r时, dp[l][r] = values[l]

            l+1 == r时, dp[l][r] = max(values[l], values[l+1])

    答案dp[0][values.length-1] > sum/2

    Time Complexity: O(n^2), 有n^2个状态需要去计算. Space: O(n^2).

    AC Java:

     1 public class Solution {
     2     /**
     3      * @param values: an array of integers
     4      * @return: a boolean which equals to true if the first player will win
     5      */
     6     public boolean firstWillWin(int[] values) {
     7         if(values == null || values.length == 0){
     8             return false;
     9         }
    10         
    11         int n = values.length;
    12         int [][] dp = new int[n][n];
    13         boolean [][] used = new boolean[n][n];
    14         int sum = 0;
    15         for(int val : values){
    16             sum += val;
    17         }
    18         return sum < 2*memorySearch(values, 0, values.length-1, dp, used);
    19     }
    20     
    21     private int memorySearch(int [] values, int l, int r, int [][] dp, boolean [][] used){
    22         if(used[l][r]){
    23             return dp[l][r];
    24         }
    25         used[l][r] = true;
    26         if(l>r){
    27             dp[l][r] = 0;
    28         }else if(l == r){
    29             dp[l][r] = values[l];
    30         }else if(l + 1 == r){
    31             dp[l][r] = Math.max(values[l], values[l+1]);
    32         }else{
    33             int pickLeft = Math.min(memorySearch(values, l+2, r, dp, used), memorySearch(values, l+1, r-1, dp, used)) + values[l];
    34             int pickRight = Math.min(memorySearch(values, l+1, r-1, dp, used), memorySearch(values, l, r-2, dp, used)) + values[r];
    35             dp[l][r] = Math.max(pickLeft, pickRight);
    36         }
    37         return dp[l][r];
    38     }
    39 }
  • 相关阅读:
    奇异值分解(SVD)详解
    深度学习中常用的优化方法
    一年了,走了一些弯路,是时候回归了,介绍下深度学习中常见的降低过拟合的方法
    softmax与logistic关系
    Java [Leetcode 387]First Unique Character in a String
    Java [Leetcode 384]Shuffle an Array
    Java [Leetcode 167]Two Sum II
    用stack实现min stack
    bootstrap, boosting, bagging 几种方法的联系
    一道常被人轻视的前端JS面试题
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6410366.html
Copyright © 2011-2022 走看看