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


    July-31-2019

    换成只能从左边或者右边拿。这个确实和Coins in a Line II有关系。
    和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样。

    public class Solution {
        public boolean firstWillWin(int[] values) {
            // write your code here
            if (values == null || values.length == 0) return false;
            if (values.length == 1) return true;
            
            int[][] dp = new int[values.length][values.length];
            dp[0][0] = values[0];
            dp[values.length-1][values.length-1] = values[values.length-1];
            
            dp[0][values.length-1] = getProfit(0, values.length-1, dp, values);
            int sum = 0;
            for (int i : values) sum += i;
            return dp[0][values.length-1] * 2 > sum;
            
        }
        
        public int getProfit(int l, int r, int[][] dp, int[] values) {
            if (l > r) return 0;
            // if (l == r) return values[l];
            if (dp[l][r] != 0) return dp[l][r];
            
            int getLeft = values[l] + Math.min(getProfit(l+1+1, r, dp, values),
                                               getProfit(l+1, r-1, dp, values));
                                               
            int getRight = values[r] + Math.min(getProfit(l+1, r-1, dp, values),
                                        getProfit(l, r-1-1, dp, values));
                                        
            dp[l][r] = Math.max(getLeft, getRight);
            return dp[l][r];
        }
    }
    
  • 相关阅读:
    numpy常用函数
    python 语法学习
    python学习:字典
    python 字符串使用
    k-近邻算法
    Numpy函数库
    机器学习初体验
    Xcode8 + iOS10Beta 权限问题崩溃的解决方法
    苹果设备全攻略
    使用 Xcode 代码块
  • 原文地址:https://www.cnblogs.com/reboot329/p/11277027.html
Copyright © 2011-2022 走看看