Test instructions is the robber can rob horses across a horse, see how to get the highest value
Dynamic programming problems need to consider the state, stage, and state transfer, this can refer to the "Dynamic Planning Classic Tutorial", some of the online, there are a lot of classic topics to explain
Dp[i] What is the maximum value to the first horse,
So all Dp[i] = max (dp[i-2]+nums[i],dp[i-1]) (where dp[0] = nums[0] dp[1] = max (nums[0],nums[1 ]);
1 classSolution {2 Public:3 intRob (vector<int>&nums) {4vector<int> dp (nums.size (),0);5 if(Nums.size () <1)6 {7 return 0;8 }9 if(Nums.size () <2)Ten { One returnnums[0]; A } -dp[0] = nums[0]; -dp[1] = max (nums[0],nums[1]); the for(inti =2; I<dp.size (); + +i) - { -Dp[i] = max (Dp[i-2] + nums[i], dp[i-1]); - } + intAns =0; - for(inti =0; I<dp.size (); + +i) + { AAns =Max (ans, dp[i]); at } - returnans; - } -};
Leetcode 198 House Robber Dynamic Planning