# Stock Liao information

— Basic knowledge of stocks|Introduction to basics of stocks|Stock learning|Basic knowledge of stocks
Mobile access：m.liaochihuo.com

# [LeetCode] 486. Winner prediction (recursion, DP)

Release Time:2021-07-04 Topic:Source code of predicting winner index formula Reading：210 Navigation：Stock Liao information > Game > [LeetCode] 486. Winner prediction (recursion, DP)

## topic description

Given an array of non-negative integers representing scores. Player 1 takes a score from either end of the array, then player 2 continues to take the score from either end of the remaining array, and then player 1 takes,.... A player can only get one score at a time, and the score is no longer available after it is taken. The game ends when there are no points left to take. The player with the most total points wins. Given an array of points, predict whether player 1 will be the winner. You can assume that each player's gameplay will maximize his score. Example 1: Input: [1, 5, 2] Output: False Explanation: At the beginning, player 1 can choose from 1 and 2. If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 has only 1 (or 2) to choose from. So, the final score of player 1 is 1 + 2 = 3, and player 2 is 5. Therefore, player 1 will never be the winner, and False is returned. Example 2: Input: [1, 5, 233, 7] Output: True Explanation: Player 1 chooses 1 at the beginning. Then player 2 must choose from 5 and 7. No matter which player 2 chooses, player 1 can choose 233. In the end, player 1 (234 points) gets more points than player 2 (12 points), so it returns True, which means that player 1 can be the winner. Reminder: 1=0.

When A first faces the interval [i...j ],

If A takes nums[ i ], then it becomes B first faces the interval [i+1...j] , The net victory of B vs. A in this interval is divided into dp[ i+1 ][ j ]; then the net victory of A vs. B should be nums[ i]-dp[ i+1 ][ j ]. If A takes nums[ j ], in the same way, the net victory of A against B is nums[ j]-dp[ i ][ j-1 ].

The larger of the above two cases is sufficient.

In summary, the state transition equation is as follows:

State transition equation: dp[ i ][ j] = max(nums[ i]-dp[ i + 1 ][ j ], nums[ j]-dp[ i ][ j-1 ])

#### Explanation 1.3

When initializing, dp[ i,i] =nums[ i ]; means If there is only one nums[ i] that can be taken, the first player takes it away, nums[ i] is the extra score

dp[ i,j] means that the first player gets nums from nums[ i] [j ], the maximum score more than the second player

For dp[i,j], the first player has two methods, one is to take the number at the beginning and the other is to take The ending number

If you take nums[ i] first, it means that the current score of the first player is nums[ i] + the opposite value of the maximum score obtained by the second player, which is dp[ i ,J] = nums[ i ]+(-dp[ i+1,j ]) where dp[ i+1,j] means the maximum score that the second player has more than the first player,

Similarly, if you take nums[ j] first, it means that the current score of the first player is the opposite of nums[ j] + the maximum score obtained by the second player, which is dp[ i ][j] = nums[ j ]+(-dp[ i,j-1 ]) where dp[ i,j-1] represents the maximum score that the second player has more than the first player

and each In one step, the first player wants to get the maximum score, and finally has a chance to win, so the final transfer equation is: dp[ i ][ j] =max{nums[ i ]+(-dp[ i+1,j] ), nums[ j ]+(-dp[ i,j-1 ])}

The last required value is dp[ 0,n-1 ], which is the number in the upper right corner of dp , To determine whether this number is greater than 0, greater than 0 means that there are more first-hand players than second-hand players, and they will win

How to fill in the form after writing the state transition equation? ## AC code

public class Solution {// State transition equation: dp[ i ][ j] = max( nums[ i]-dp[ i + 1 ][ j ], nums[ j]-dp[ i ][ j-1 ]) public boolean PredictTheWinner(int[] nums) {int len ​​= nums.length; int[] [] dp = new int[ len ][ len ]; // dp[ i ][ j ]: As a first mover, the relative score that can be obtained by choosing in the interval nums[ i..j] for (int i = 0; i = 0; i --) {dp[ i ][ j] = Math.max(nums[ i]-dp[ i + 1 ][ j ], nums[ j]-dp[ i ][ j-1 ]);}} return dp[ 0 ][ len-1] >= 0; }}

Article Url:https://www.liaochihuo.com/info/596427.html

Label group:[dp

• ### Ten billion private equity Linyuan responded: Why are you not optimistic about banks and buy bank index?

To invest in stocks, look at the research reports of Golden Kylin analysts, authoritative, professional, timely and comprehensive, to help you tap po...

• ### Big turn over! Linyuan's 20 products earned an average of 19%, and private equity made a huge profit of 1100% this year! _ invest

Its not easy to say Lin Yuan. In the first half of last year, everyone was confused by the sweater war. He made a lot of money from investing in pharm...

• ### Lin Yuan, Dan Bin, Feng Liubaping the hot search list of fund managers are still looking for new investment targets in the pharmaceutical and technology tracks

According to private equity Pai Pai.com data, in the first half of this year, Linyuan Investment Linyuan, Oriental Harbor Dan Bin, Gao Yi Asset Feng L...

• ### Linyuan's three major funds besiege a bull stock, tens of billions of private equity nuggets "just need the mouth" economy

With the start of the Ashare bull market in 2020, Shenzhen Linyuan Investment Management Co., Ltd.The chairman of the company Lin Yuan is very...

• ### The latest ranking of tens of billions of private equity! Perfect circle to win! Linyuan, Dan Bin, Xingshi and other veteran private equity companies are on the list! _ Investment _ Income _

The grand slam of the list refers to the last year, the last three years, and the last five years as of the end of January The companys income is ...

• ### The three major funds of Linyuan are besieging a bull stock, and the tens of billions of private equity nuggets "just need their mouths" economy

China Times reporter Wang Zhaohuan reported in Beijing With the 2020 Ashare bull market With the launch of Shenzhen Linyuan Investment Management Co...

• ### This leading stock was reduced by Linyuan's clearance! Shares are up 110% this year! Netizens collapse: leaving only a piece of chicken feathers​

In the third quarter of this year, Lin Yuan had _ sh600436>Pien Tze Huangs position adjustment, or the biggest adjustment since Linyuan Investment...

• ### Private equity boss Lin Yuan angrily angered investors: "You deserve to not apologize to the poor if you lose money!"

To invest in stocks, look at the research reports of Golden Kylin analysts, authoritative, professional, timely and comprehensive, to help you tap po...