• 651. 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.

    Example 1:

    Input: N = 3
    Output: 3
    Explanation: 
    We can at most get 3 A's on screen by pressing following key sequence:
    A, A, A
    

     

    Example 2:

    Input: N = 7
    Output: 9
    Explanation: 
    We can at most get 9 A's on screen by pressing following key sequence:
    A, A, A, Ctrl A, Ctrl C, Ctrl V, Ctrl V

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    贪心算法可以从除法 and 乘法的角度来思考

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [一句话思路]:

    因为要做全选、拷贝、粘贴:三个数以内要进行相乘

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    复制粘贴不是乘法就是除法

    [复杂度]:Time complexity: O(n) Space complexity: O(1)

    [算法思想:迭代/递归/分治/贪心]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

     [潜台词] :

    class Solution {
        public int maxA(int N) {
            int max = N;
            for (int i = 1; i <= N - 3; i++) {
                max = Math.max(max, maxA(i) * (N - i - 1));
            }
            return max;
        }
    }
    View Code
  • 相关阅读:
    JavaScript函数调用
    JS数据类型&&typeof&&其他
    JavaScript闭包底层解析
    test
    C# 网页自动填表自动登录 .
    C#中没有id 没有name C#怎么点击按钮
    网页中403错误的含义
    C# 按钮置顶和隐藏
    C# webBrowser 屏蔽网页JS脚本错误弹窗
    HTML5 canvas globalCompositeOperation 设置绘图的顺序
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9527679.html
Copyright © 2020-2023  润新知