GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/src/StoneGame.java at master · LeBW/leetcode Expand file tree/ Copy pathStoneGame.javaMore file actions 42 lines (40 loc) · 1.1 KB / Copy pathStoneGame.javaFile metadata and controls42 lines (40 loc) · 1.1 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 /** * 877. Stone Game * @author LBW * 博弈DP问题。 */ public class StoneGame { public boolean stoneGame(int[] piles) { int len = piles.length; Pair[][] dp = new Pair[len][len]; // init for (int i = 0; i < len; i++) { dp[i][i].fir = piles[i]; dp[i][i].sec = 0; } // fill the dp array for (int j = 1; j < len; j++) { for (int i = j - 1; i >= 0; i--) { int left = piles[i] + dp[i+1][j].sec; int right = piles[j] + dp[i][j-1].sec; if (left >= right) { // 先手选择左边 dp[i][j].fir = left; dp[i][j].sec = dp[i+1][j].fir; } else { //先手选择右边 dp[i][j].fir = right; dp[i][j].sec = dp[i][j-1].fir; } } } return dp[0][len-1].fir > dp[0][len-1].sec; } class Pair { int fir; int sec; Pair(int fir, int sec) { this.fir = fir; this.sec = sec; } } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |