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 }
  • 相关阅读:
    5M1E,软件质量管理最佳解决方案
    Laravel 刚创建的项目 API 无法使用 Session,是没有在Api的请求内开启Session
    python添加默认模块搜索路径
    [传纸条]
    [miller_rabin]
    国内rog游戏手机2与国际版本的 差别
    windows下依端口查进程
    gor phone 2 优缺点
    IDA pro 6.8 导入cpu解析模块报错 ImportError: No module named ida_ bytes
    JEB 4.5 新增 RISC-V 反编译器支持
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6410366.html
Copyright © 2011-2022 走看看