每次从一开始或者最后拿,拿多的赢
#include<iostream> using namespace std; int stone[10]; int dp[10][10];//从i到j两人数量差的最大值 int main() { int n; cin>>n; for(int i=0;i<n;i++) cin>>stone[i]; for(int i=0;i<n;i++) dp[i][i]=stone[i]; for (int i = n - 2; i >= 0; i--) for (int j = i + 1; j < n; j++) dp[i][j] = max(stone[i] - dp[i + 1][j], stone[j] - dp[i][j - 1]); cout<<dp[0][n - 1]; }
一维数组
class Solution { public: bool stoneGame(vector<int>& piles) { int length = piles.size(); auto dp = vector<int>(length); for (int i = 0; i < length; i++) { dp[i] = piles[i]; } for (int i = length - 2; i >= 0; i--) { for (int j = i + 1; j < length; j++) { dp[j] = max(piles[i] - dp[j], piles[j] - dp[j - 1]); } } return dp[length - 1] > 0; } }; 作者:LeetCode-Solution 链接:https://leetcode.cn/problems/stone-game/solution/shi-zi-you-xi-by-leetcode-solution/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
标签:10,stone,piles,int,石子,length,dp From: https://www.cnblogs.com/weinan030416/p/17103556.html