nikunjsoni

818

May 26th, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int racecar(int target){
  4.         int dp[target+1];
  5.         memset(dp, 0, sizeof dp);
  6.    
  7.         for(int i = 1; i <= target; i++){
  8.             dp[i] = INT_MAX;
  9.             int m = 1, j = 1;
  10.             for(; j < i; j = (1 << ++m)-1){
  11.                 for(int q = 0, p = 0; p < j; p = (1 << ++q)-1){
  12.                     dp[i] = min(dp[i], m + 1 + q + 1 + dp[i-(j - p)]);
  13.                 }
  14.             }
  15.             dp[i] = min(dp[i], m+(i == j ? 0 : 1 + dp[j - i]));
  16.         }
  17.         return dp[target];
  18.     }
  19. };
Advertisement
Add Comment
Please, Sign In to add comment