Imagine you have a special keyboard with the following keys:

Key 1: (A): Prints 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.

思路:

要想N步生成最多个A,可在N-2步的时候,Ctrl A,N-1步的时候,Ctrl C,第N步的时候Ctrl V,这样就能将N-3步生成的A的个数,翻倍。

如何确定在第几步Ctrl A,然后再Ctrl C、Ctrl V呢,需要依次判断第i-3步之前的步骤。

得到递推公式 dp[i] = max(dp[i],dp[i-j-1]);dp[i]表示第i步生成的最多的A的个数。

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];
}

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

[leetcode-651-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] 650. 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 (Medium)

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

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

    [抄题]: Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on scre ...

  5. LeetCode 650 - 2 Keys Keyboard

    LeetCode 第650题 Initially on a notepad only one character 'A' is present. You can perform two operati ...

  6. [LeetCode] 4 Keys Keyboard 四键的键盘

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

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

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

  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 4 Keys Keyboard

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

  10. Leetcode 之 Keys Keyboard

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

随机推荐

  1. data-ng-disabled指令

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  2. 数据库——MySQL——多表查询

    这里多表,为了方便我只建了两张表,更复杂的表间也就是这些东西,就是复杂程度不一样. 数据源准备 建立一个学生表,和一个班级表 # 建立学生表 create table student( id int ...

  3. TIDB1 —— TiDB简介

    TiDB 兼容 MySQL,支持无限的水平扩展,具备强一致性和高可用性. TiDB 具备如下核心特点: 1 高度兼容 MySQL  大多数情况下,无需修改代码即可从 MySQL 轻松迁移至 TiDB, ...

  4. HDFS的存储策略

    本文介绍hdfs的存储策略 内容译自:http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-hdfs/ArchivalStor ...

  5. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column …… 出现这个异常的很大可能性是 数据库是没有问题的 ...

  6. YII2.O学习三 前后台用户数据表分离

    之前我们完成了Advanced 模板安装,也完成了安装adminlte 后台模板,这一步是针对前端和后台用户使用不同的数据库表来管理,做到前后台用户分离的效果: 复制一张user数据表并重命名为adm ...

  7. centos编译安装rabbitmq

    安装环境 [root@VM_12_50_centos rabbitmq]# uname -a Linux VM_12_50_centos 3.10.0-514.21.1.el7.x86_64 #1 S ...

  8. ctf题目writeup(2)

    2019.1.29 题目地址: https://www.ichunqiu.com/battalion 1. 点开链接: include "flag.php";$a = @$_REQ ...

  9. labview在编写程序框图中遇到的一些布尔按钮控制布尔指示灯问题

                      上图布尔控件按下,数据0x04成功发送给下位机,布尔灯不亮. ............... ............. ........... 下图布尔控件按下, ...

  10. 39-Role以及Claims授权

    asp.net core多鼓励使用claims授权 1-使用role授权 在类或方法上贴上Roles,这样就知道有user的角色才可以访问 [Authorize(Roles="user&qu ...