jayati

Special Keyboard(Dynamic Programming)

Sep 28th, 2023
1,182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. //{ Driver Code Starts
  2. // Initial Template for C++
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. // } Driver Code Ends
  8. // User function Template for C++
  9.  
  10. class Solution{
  11. public:
  12.     long long int optimalKeys(int N){
  13.         // code here
  14.         vector<int> dp(N+1,0);
  15.         if(N<=6)
  16.         {
  17.             return N;
  18.         }
  19.         for(int i=1;i<=6;i++)
  20.         {
  21.             dp[i]=i;
  22.         }
  23.         for(int i=7;i<=N;i++)
  24.         {
  25.             for(int j=i-3;j>=1;j--)
  26.             {
  27.                int curr = dp[j]*(i-j-1);
  28.                if(curr>dp[i])
  29.                {
  30.                    dp[i]=curr;
  31.                }
  32.             }
  33.         }
  34.         return dp[N];
  35.     }
  36. };
  37.  
  38. //{ Driver Code Starts.
  39.  
  40. int main(){
  41.     int t;
  42.     cin>>t;
  43.     while(t--){
  44.         int N;
  45.         cin>>N;
  46.        
  47.         Solution ob;
  48.         cout<<ob.optimalKeys(N)<<"\n";
  49.     }
  50.     return 0;
  51. }
  52. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment