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

Note:

  1. 1 <= N <= 50
  2. Answers will be in the range of 32-bit signed integer.

这道题给了我们四个操作,分别是打印A,全选,复制,粘贴。每个操作都算一个步骤,给了我们一个数字N,问我们N个操作最多能输出多个A。我们可以分析题目中的例子可以发现,N步最少都能打印N个A出来,因为我们可以每步都是打印A。那么能超过N的情况肯定就是使用了复制粘贴,这里由于全选和复制要占用两步,所以能增加A的个数的操作其实只有N-2步,那么我们如何确定打印几个A,剩下都是粘贴呢,其实是个trade off,A打印的太多或太少,都不会得到最大结果,所以打印A和粘贴的次数要接近,最简单的方法就是遍历所有的情况然后取最大值,打印A的次数在[1, N-3]之间,粘贴的次数为N-2-i,加上打印出的部分,就是N-1-i了,参见代码如下:

解法一:

class Solution {
public:
int maxA(int N) {
int res = N;
for (int i = ; i < N - ; ++i) {
res = max(res, maxA(i) * (N - - i));
}
return res;
}
};

这道题也可以用DP来做,我们用一个一维数组dp,其中dp[i]表示步骤总数为i时,能打印出的最多A的个数,初始化为N+1个,然后我们来想递推公式怎么求。对于dp[i]来说,求法其实跟上面的方法一样,还是要遍历所有打印A的个数,然后乘以粘贴的次数加1,用来更新dp[i],参见代码如下:

解法二:

class Solution {
public:
int maxA(int N) {
vector<int> dp(N + , );
for (int i = ; i <= N; ++i) {
dp[i] = i;
for (int j = ; j < i - ; ++j) {
dp[i] = max(dp[i], dp[j] * (i - j - ));
}
}
return dp[N];
}
};

这道题还有个O(1)时间复杂度的解法,好像利用了数学知识,不过博主貌似没太理解,参见这个帖子,哪位大神给博主讲解一下?

类似题目:

2 Keys Keyboard

参考资料:

https://discuss.leetcode.com/topic/97764/o-1-time-o-1-space-c-solution-possibly-shortest-and-fastest

https://discuss.leetcode.com/topic/97628/java-4-lines-recursion-with-step-by-step-explanation-to-derive-dp

[LeetCode] 4 Keys Keyboard 四键的键盘的更多相关文章

  1. [LeetCode] 651. 4 Keys Keyboard 四键的键盘

    Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...

  2. [LeetCode] 2 Keys Keyboard 两键的键盘

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...

  3. [LeetCode] 650. 2 Keys Keyboard 两键的键盘

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...

  4. LeetCode 4 Keys Keyboard

    原题链接在这里:https://leetcode.com/problems/4-keys-keyboard/description/ 题目: Imagine you have a special ke ...

  5. Leetcode 之 Keys Keyboard

    1. 2 Keys Keyboard 先把dp的最小不走都设置为无穷大(Integer.MAX_VALUE),初始化条件:dp[0] = dp[1] = 0,状态转移方程为dp[i] = Math.m ...

  6. Leetcode 650.只有两个键的键盘

    只有两个键的键盘 最初在一个记事本上只有一个字符 'A'.你每次可以对这个记事本进行两种操作: Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的). Past ...

  7. Java实现 LeetCode 650 只有两个键的键盘(递归 || 数学)

    650. 只有两个键的键盘 最初在一个记事本上只有一个字符 'A'.你每次可以对这个记事本进行两种操作: Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的). ...

  8. LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion

    1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

  9. [leetcode] 650. 2 Keys Keyboard (Medium)

    解法一: 暴力DFS搜索,对每一步进行复制还是粘贴的状态进行遍历. 注意剪枝的地方: 1.当前A数量大于目标数量,停止搜索 2.当前剪贴板数字大于等于A数量时,只搜索下一步为粘贴的状态. Runtim ...

随机推荐

  1. JVM内存管理概述与android内存泄露分析

    一.内存划分 将内存划分为六大部分,分别是PC寄存器.JAVA虚拟机栈.JAVA堆.方法区.运行时常量池以及本地方法栈. 1.PC寄存器(线程独有):全称是程序计数寄存器,它记载着每一个线程当前运行的 ...

  2. python 函数基础 定义

    一.函数介绍 1.为什么要有函数? 没有函数的代码组织结构不清晰,可读性差. 代码冗余 管理维护难度大,扩展性 2.什么是函数? 具备某一个功能的工具就是程序中的函数. 事先准备工具的过程就是:函数的 ...

  3. Python下载图片小程序

    欢迎大侠们指正批评 思路: 1.引入相关的python文件(import re  import urllib) 2.读取对应网页的html文件(使用 urllib) def getHtml(url): ...

  4. 多目标跟踪(MOT)论文随笔-SIMPLE ONLINE AND REALTIME TRACKING (SORT)

    网上已有很多关于MOT的文章,此系列仅为个人阅读随笔,便于初学者的共同成长.若希望详细了解,建议阅读原文. 本文是使用 tracking by detection 方法进行多目标跟踪的文章,是后续de ...

  5. Android中的layout_gravity和gravity的区别

    在Android的布局中,除了padding和margin容易弄混之外,还有layout_gravity和gravity.按照字面意思来说,layout_gravity就是相对于layout来设置的. ...

  6. [W班]第二次结对作业成绩评价

    作业地址: https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/1016 作业要求: 1.代码具有规范性. ...

  7. 网络1711c语言第3次作业总结

    作业地址:https://edu.cnblogs.com/campus/jmu/JMUC--NE17111712/homework/1166 总结 1.评分细则 评分注意事项 注意用Markdown语 ...

  8. Beta冲刺NO.7

    Beta冲刺 第七天 昨天的困难 昨天的困难在一些多表查询上,不熟悉hibernate的套路,走了很多弯路. 第一次使用图表插件,在图表的显示问题上花了一定的时间. 对于页面绑定和后台数据自动填充的理 ...

  9. SELinux与进程管理

  10. LDAP的用户需求

    使用LDAP(ApacheDS)构建统一认证服务(SSO单点登录)   构建团队协作的体系,需要涉及很多个系统,如SVN.Jenkins.Trac.Nexus等,而一般而言每个系统均有其用户体系,当我 ...