# Stock Liao information

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

# "Predicting Winners"-the road to optimization from recursion to dynamic programming

Release Time:2021-07-04 Topic:Imitate the predictive winner index formula Reading：19 Navigation：Stock Liao information > Game > "Predicting Winners"-the road to optimization from recursion to dynamic programming

This is a topic of Likou:

If you don’t know how to do it, you can go to see the solution for yourself, it's very detailed.

Here I mainly write down my own course from recursive optimization to dynamic programming (spatial optimization). (The code can pass through the oj of Likou)

## Recursion:

1 class Solution { 2 public : 3 int dfs(vector&nums, int left, int right) 4 { // The meaning of dfs here Is the difference between the score of the player currently operating and the score of another player 5 if(left == right) 6 return nums[ left ]; 7 return max(nums[ left]-dfs( nums,left+ 1,right),nums[ right ]-dfs(nums,left,right- 1 )); 8 } // Because "the player’s play style will maximize his score, the current player tries his best to choose a strategy with a higher score than the opponent 9 bool PredictTheWinner(vector& nums) { 10 return dfs(nums, 0,nums.size()- 1) >= 0; // The difference between one's own score and the other's score The difference is not less than 0. Your own score is not less than the opponent's score 11 } 12 };

Time efficiency is very low, so upgrade:

## Memoized recursion :

1 class Solution { 2 public : 3 int memory[ 20 ][ 20 ]; // Since the data range has been given, directly define the size of the array 4 int dfs(vector&nums, int left, int right) 5 { // The meaning of dfs here is the score of the current player and another player The difference between the scores 6 if(memory[ left ][ right ]!=- 1 ) 7 return memory[ left ][ right ]; 8 if(left == right) 9 { 10 memory[ left ][ right ]= nums[ left] ; 11 return nums[ left ]; 12 } 13 int ans = max(nums[ left]-dfs(nums,left+ 1,right),nums[ right ]-dfs(nums, left,right- 1 )); 14 memory[ left ][ right] = ans; 15 return ans; 16 } // Since "the player’s gameplay will maximize his score, so the current Players try their best to choose strategies that score higher than their opponents 17 bool PredictTheWinner(vector& nums) { 18 memset(memory,- 1, sizeof (memory)); 19 return dfs(nums, 0,nums.size()- 1) >= 0; // The difference between your own score and the opponent's score is not less than 0. Then your own score is not less than the opponent's score 20 } 21 20 } 21 span> };

The efficiency is greatly improved

Then by summarizing the recursive recursion formula, it is not difficult to think of using dynamic programming instead of recursion:

## Dynamic programming:

1 class Solution { 2 public : 3 int dp[ 20] [ 20 ]; 4 bool PredictTheWinner(vector& nums) { 5 for( int i = 0;i

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

Label group:[dynamic programming] [Recursion] [dfs

• ### 21 research reports recommend securities industry leaders CITIC Securities ranks among the top three A-shares

During the annual report disclosure period, brokers span>Annual report reviews research report are also followed up in time. Last week, the secu...

• ### IF forward contract discounts have significantly converged, institutions are optimistic about the long-term performance of blue chip stocks

IF1803 daily candlestick chart CSI 300 index daily candlestick chart ⊙Reporter Shi Tingqi ○Editor Chen Yu Since the beginning of this year, the con...

• ### With an average of 6 years in the industry, 43% has covered the science and technology innovation board, and foreign investment has become the largest increase in services. Strengthen indust

On December 25, the annual meeting of New Wealth Analysts was held in Xi’an. Last, New Fortune released the 2019 China Securities Research Industry Va...

• ### Highlights of the headlines of the four major domestic securities newspapers and important financial media on April 27

Original title On April 27, the headlines of the four major domestic securities newspapers and important financial media were summarized. Source East...

• ### Highlights of the headlines of the four major securities newspapers on October 27

China Securities Journal Raised approximately US\$34.4 billion, Ant launches the world’s largest IPO October On the evening of the 26th, the issuance...

• ### China Securities Journal: Overview of major events in China's securities market in 2017

Overview of major events in China’s securities market in 2017 China Securities Journal 2017 People’s Bank of China Working Conference on the 5th To...

• ### Dacheng Fund Xu Yan: Practicing value investment based on the understanding of society and the times

If you make a list of Ashare public fund managers who truly practice value investment, Xu Yanyi of Dacheng Fund will be listed among them. Since joini...

• ### Highlights of the headlines of the four major domestic securities newspapers and important financial media on October 20

Original title On October 20, the headlines of the four major domestic securities newspapers and important financial media were summarized. Source Ea...