Maximum number of characters using keystrokes A, Ctrl+A, Ctrl+C and Ctrl+V

前端 未结 14 1493
忘掉有多难
忘掉有多难 2020-12-07 07:03

This is an interview question from google. I am not able to solve it by myself. Can somebody shed some light?

Write a program to print the sequence of keystrokes suc

14条回答
  •  暖寄归人
    2020-12-07 07:36

    public int dp(int n) 
    {
        int arr[] = new int[n];
        for (int i = 0; i < n; i++)
            arr[i] = i + 1;
        for (int i = 2; i < n - 3; i++) 
        {
            int numchars = arr[i] * 2;
            int j = i + 3;
            arr[j] = Math.max(arr[j], numchars);
            while (j < n - 1) 
            {
                numchars = numchars + arr[i];
                arr[++j] = Math.max(arr[j], numchars);
            }
        }
        return arr[n - 1];
    }
    

提交回复
热议问题