zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    LINUX_bash
    【c++】必须在类初始化列表中初始化的几种情况
    Hadoop 学习笔记 (八) hadoop2.2.0 测试环境部署 及两种启动方式
    hadoop各版本下载
    mapreduce (六) MapReduce实现去重 NullWritable的使用
    hadoop 生态系统版本对应问题
    mapreduce (五) MapReduce实现倒排索引 修改版 combiner是把同一个机器上的多个map的结果先聚合一次
    mapreduce (四) MapReduce实现Grep+sort
    ctr预估模型
    mapreduce (七) 几个实例
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9527679.html
Copyright © 2011-2022 走看看