码迷,mamicode.com
首页 > 其他好文 > 详细

651. 4 Keys Keyboard复制粘贴获得的最大长度

时间:2018-08-24 10:47:39      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:分治   复制   ted   debug   amp   贪心算法   ESS   技术   with   

[抄题]:

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

 

651. 4 Keys Keyboard复制粘贴获得的最大长度

标签:分治   复制   ted   debug   amp   贪心算法   ESS   技术   with   

原文地址:https://www.cnblogs.com/immiao0319/p/9527679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!