# Stock Liao information

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

# 486. Winner prediction

Release Time:2021-07-04 Topic:Imitate the predictive winner index formula Reading：18 Navigation：Stock Liao information > Game > 486. Winner prediction

For this question, you can use

recursion and

dynamic return in the solution Two ideas are solved, and the one chosen here is realized by moving back.

The first thing to note is that in the problem, it is assumed that each player's gameplay will maximize his score, and player 1 is the first mover.

public boolean PredictTheWinner( int[] nums) { int[ ][] dp = new int[ nums.length ][ nums.length ]; dp[ dp.length- 1 ][ dp.length- 1] = nums [nums.length- 1 ]; for ( int left = dp.length- 2; left >= 0; left--) { for ( int right = left; right right++) { if ( left == right) {dp[ left ][ right] = nums[ left ];} else { int pickLeft = nums[ left]-dp[ left + 1 ][ right ]; int pickRight = nums[ right]-dp[ left ][ right- 1 ]; dp[ left ][ right] = Math.max(pickLeft, pickRight);}}} return dp[ 0 ][ nums.length- 1] >= 0;}

The sum of the scores obtained by both parties is certain. The dynamic programming algorithm here considers twoThe difference of the person. In other words, the sum of the points scored by player 1 minus the sum of points scored by player 2 will win if it is greater than 0 in the end.

The above is the AC algorithm. For the auxiliary array dp[ left ][ right ], there is a transfer equation:

dp[ left ][ right] = max( nums[ left]-dp[ left + 1 ][ right ], nums[ right]-dp[ left ][ right-1 ])

this It means that player 1 and player 2 are in the interval [left, right ], the difference between the total score of player 1 and the total score of player 2. Among them, the value of dp[ left ][ right] is dynamically dependent on dp[ left + 1 ][ right] and dp[ left ][ right-1 ]. The specific relationship is as above. If player 1 chooses the leftmost score at this time , Player 2 can only dynamically choose from the leftmost + 1 to the rightmost interval. At this time, Player 2 will also choose the best answer according to the remaining interval; when Player 1 chooses the rightmost score, it is also in this way.

When player 1 chooses the leftmost score, there are nums[ left]-dp[ left + 1 ][ right ], which means that player 1 chooses the leftmost score nums[ left ], and then subtract Go to

the difference between the best choice obtained by player 2 in the remaining interval [left-1 ][ right] and the total score of player 1 dp[ left + 1 ][ right ], this is the player 1 and player 2 are in the interval [left, right ]. The difference between the two is the total score difference. Among them, the subtracted dp[ left + 1 ][ right] (or dp[ left ][ right-1]) dynamically represents the difference between player 1 and player 2 which are mutually exclusive.

Regarding the above paragraph, you may have doubts. Here is a specific example to explain:

For the array {1,2,3}, there are: p> dp[ 1,3] = max(1-dp[ 2 ][ 3 ], 3-dp[ 1 ][ 2 ])dp[ 2,3] = max(3-dp[ 2 ][ 2 ], 2-dp[ 3 ][ 3 ]) = max(3-2, 2-3) = 3-2dp[ 1,2] = max(1-dp[ 2 ][ 2 ], 2 -dp[ 1 ][ 1 ]) = max(1-2, 2-1) = 2-1then dp[ 1,3] = max(1-(3-2), 3-(2-1)) = max(1-3 + 2, 3-2 + 1)

1-(3-2) further becomes 1-3 + 2 where the numbers 1 and 2 corresponding to the positive sign are

Player 1The number 3 corresponding to the negative sign is the choice of

Player 2, and the result of 1-3 + 2 is the difference between the total score of player 1 minus the total score of player 2, and you need to pay attention to max(1-dp[ 2 ][ 2 ], 2-dp[ 1 ][ 1 ]) where dp[ 2 ][ 2] represents in the partial interval {2} of the overall array {1,2,3} The difference between the total score of player 2 minus the total score of player 1, because after player 1 chooses 1 in the partial interval {1,2} in the overall array {1,2,3}, player 2 has only 2 to choose from Now,

dp[ 2 ][ 2] is for player 2 at this time.

But if the entire array has only {1,2}, then dp[ 2 ][ 2] is for player 1.

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

Label group:[dp

• ### How can long-term profit earners avoid being investigated by the Securities Regulatory Commission?

The stock market is risky, and investment needs to be cautious. 7 losses, 2 draws and 1 profit have already written the stock market risks vividly. H...

• ### Seeking U.S. stock brokers? Which one has the lowest commission?

Is there any commission lower than 0? Directly list more than a dozen Hong Kong and US stock brokers, and the lowest one can achieve zero commissi...

• ### At least one commission is charged

❶ Stock trading commissions, why do all securities companies have this rule starting at a minimum of RMB 5? Basically all companies have such a rule....

• ### Foreign investment officially enters the market, and the brokerage structure is about to be reshuffled! Sheep enter the tiger’s mouth, or pretend to be a pig and eat a tiger? _ Financial ope

In June 2018, A shares were officially included in MSCI. In June 2019, the ShanghaiLondon Stock Connect was officially launched. At the same time, ...

• ### Brokerage Transformation: GF Surgery Salesman, Galaxy 100 C-type Chargers

Sina Finance Client China Securities Regulatory Commission’s New Deal activates eight key words of Ashare IPO reform The Wealth Management Departmen...

• ### Brokerage business has declined so badly, but do not trust the sky to eat? Take a look at the four new ways of listed brokers

In the first half of this year, the significant yearonyear decline in the performance of brokerage firms has been within market expectations, and t...

• ### Securities industry: public funds, the pattern and outlook of the blue ocean market

Highlights Promote the transition to buyer investment advisory, which is expected to optimize the profitability and structure of asset management in...

• ### GF Securities-Dual Drives of Asset Management and Investment Banking

Investment banking business IPO directly drives the expected income increase of the corresponding underwriters and boosts the performance of securit...