Special Keyboard | Practice | GeeksforGeeks

Imagine you have a special keyboard with the following keys: 
Key 1:  Prints 'A' on screen
Key 2: (Ctrl-A): Select screen
Key 3: (Ctrl-C): Copy selection to buffer
Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. 
Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. 

Example 1:
Input: N = 3
Output: 3
Explaination: Press key 1 three times.

Example 2:
Input: N = 7
Output: 9
Explaination: The best key sequence is 
key 1, key 1, key 1, key 2, key 3,
key4, key 4.

Your Task:
You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations.

Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(N)

Constraints:
1 < N < 75
+1
bokonist3 weeks ago
O(N) space, O(N) time C++ solution with comments
.
.
#define lli long long int
long long int optimalKeys(int N)
{
    if(N<=6) 
        return N;
    lli screenSize[N+1], scp, scpp, scppp;
    screenSize[0]=0;
    for(int i=1;i<=6;i++)
        screenSize[i]=i;
    for(int i=7;i<=N;i++)
    {
        // option 1 is select, copy, paste the screensize at cur-3 using 3 keystrokes, doubling the size at cur-3
        scp = 2* screenSize[i-3];
        //option 2 is scpp the screensize at cur-4, using 4 keystrokes, tripling the size at cur-4
        scpp = 3* screenSize[i-4];
        //option 3 is scppp the screensize at cur-5, using 5 keystrokes, quadrupling the size at cur-5
        scppp = 4 * screenSize[i-5];
        screenSize[i] = max({scp, scpp, scppp}); //select the maximum from these 3
    }
    return screenSize[N];
}
0
putyavka3 weeks ago
Time O(N), Space O(N)
class Solution{
    struct Entry {
        long long f, g;
    };
public:
    long long int optimalKeys(int N){
        vector<pair<Entry, Entry>> F(N + 4);
        F[1] = {{1, 1},{}};
        for (int i = 1; i < N; i++) {
            auto& p = F[i];
            auto& p1 = F[i + 1];
            auto& p3 = F[i + 3];
            if (p.first.f + p.first.g > p.second.f + p.second.g)
                p1.first = { p.first.f + p.first.g, p.first.g };
            else
                p1.first = { p.second.f + p.second.g, p.second.g };
            if (p.first.f > p.second.f)
                p3.second = { p.first.f << 1, p.first.f };
            else
                p3.second = { p.second.f << 1, p.second.f };
        }
        return max(F[N].first.f, F[N].second.f);
    }
};
+1
shyamprakash8071 month ago
Python solution :
Time taken : 0.0 / 5.7
 
class Solution:   def optimalKeys(self, N):       # code here       if N <= 6:           return N       dp = [i if i<=6 else 0 for i in range(N+1)]       for i in range(7, N+1):           c = 2           for j in range(i-3, 0, -1):               if dp[j]*c >= dp[i]:                   dp[i] = dp[j]*c               else:                   break               c += 1       return dp[N]                     
0
shashankrustagii4 months ago
//https://www.youtube.com/watch?v=c_y7H7qZJRU
class Solution{
public:
    long long int optimalKeys(int N)
    {
        if(N <=6) return N;
        vector<int> dp(N+1,0);
        for(int i=1;i<=6;i++) dp[i]=i;
        for(int i=7;i<=N;i++){
            for(int j=i-3;j>=1;j--){
                int curr = dp[j]*(i-j-1);
                dp[i]=max(curr,dp[i]);
            }
            
        }
        return dp[N];
    }
};
+1
shashankrustagii4 months ago
 
//https://www.youtube.com/watch?v=c_y7H7qZJRU
class Solution{
public:
    long long int optimalKeys(int N)
    {
        if(N <=6) return N;
        vector<int> dp(N+1,0);
        for(int i=1;i<=6;i++) dp[i]=i;
        for(int i=7;i<=N;i++){
            for(int j=i-3;j>=1;j--){
                int curr = dp[j]*(i-j-1);
                dp[i]=max(curr,dp[i]);
            }
            
        }
        return dp[N];
    }
};
0
shashankrustagii
This comment was deleted.
0
amrit_kumar4 months ago
c++ simple iterative
long long int optimalKeys(int N)
    {
        if(N <=6) return N;
        
        int screen[N];
        
        for(int i=1;i<=6;i++)
        screen[i-1] = i;
        
        for(int i=7;i<=N;i++)
        {
            int mul = 2;
            int res = 0;
            for(int j=i-3;j>=1;j--)
            {
                int cur = mul *screen[j-1];
                res = max(cur,res);
                mul++;
            }
            screen[i-1] = res;
        }
        return screen[N-1];
        
    }
0
geminicode4 months ago
This is pure maths .. try solving it on paper you'll get it .
If stuck look at this solution.
#1. prints 1 char
        #2. after 2 more presses doubles the char on the screen.
        # max(dp[i], max we can get using j + (N- j we used -2 traversal)*jleft
        # we will use dp
        dp = [i for i in range(N+1)]
        dp[1] = 1
        for i in range(2,N+1):
            for j in range(1,i):
                if (N-j-2) > 0:
                    dp[i] = max(dp[i], dp[j]+ ((i-j-2)*dp[j]))
        return dp[N]
+2
ankitkrrrrr6 months ago
class Solution{public:   long long int optimalKeys(int N){       if(N<=6) return N;     long long int dp[N+1];     dp[0]=0;     for(int i=1;i<=6;i++)       dp[i]=i;          for(int i=7;i<=N;i++)       dp[i]=max({2*dp[i-3],3*dp[i-4],4*dp[i-5]});          return dp[N];   }};
0
ahmadashad07867 months ago
long long int optimalKeys(int n){       // code here       if(n<=6){           return n;       }       int dp[n+1];       for(int i=0; i<=n; i++){           dp[i]=i;       }       for(int i=7; i<=n; i++){           for(int j=i-3; j>=1; j--){               int curr=dp[j]*(i-j-1);               dp[i]=max(dp[i],curr);           }       }              return dp[n];          }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.