4 Keys Keyboard

题目
Imagine you have a special keyboard with the following keys:

Key 1: (A): Print one 'A' on screen.

Key 2: (Ctrl-A): Select the whole 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.

Now, you can only press the keyboard for N times (with the above four keys), find out the maximum numbers of 'A' you can print on screen.

答案

class Solution {
    public int maxA(int N) {
        // Can N fit in at least 4 keys?
        if(N < 4) return N;
        int[] dp = new int[N+1];
        for(int i = 1; i <= N; i++) {
            dp[i] = i;
            for(int j = 3; j < i; j++)
                dp[i] = Math.max(dp[i], dp[i - j] * (j - 1));
        }
        return dp[N];
    }
}

你可能感兴趣的:(4 Keys Keyboard)