GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/src/StoneGame.java at master · LeBW/leetcode

Expand file tree

/

StoneGame.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
42 lines (40 loc) · 1.1 KB
/

StoneGame.java

Copy path

File metadata and controls

42 lines (40 loc) · 1.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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.