原题链接在这里:https://leetcode.com/problems/4-keys-keyboard/description/

题目:

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.

题解:

DP问题.

初始化, dp[i] = i. i在[1,N]就是直接输入'A'.

状态转移, dp[i] = Math.max(dp[i], (i-j-1)*dp[j]), j在[1,i-3]区间内.  Ctrl A, Ctrl C, Ctrl V用了3个操作,所以j是到i-3. 本来已经有了d[j]个'A', 三个操作做完又加了dp[j]个'A', 还剩下i-3-j个操作,剩下的每一次都加dp[j]个'A'. 总共dp[j] + dp[j] + (i-3-j)*dp[j] = (i-j-1)*dp[j].

答案dp[N].

Time Complexity: O(N^2).

Space: O(N).

AC Java:

 class Solution {
public int maxA(int N) {
int [] dp = new int[N+1];
for(int i = 1; i<=N; i++){
dp[i] = i;
for(int j = 1; j<=i-3; j++){
dp[i] = Math.max(dp[i], (i-j-1)*dp[j]);
}
}
return dp[N];
}
}

类似2 Keys Keyboard.

LeetCode 4 Keys Keyboard的更多相关文章

  1. [LeetCode] 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 之 Keys Keyboard

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

  4. 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 ...

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

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

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

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

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

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

  8. LC 650. 2 Keys Keyboard

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

  9. LeetCode 650 - 2 Keys Keyboard

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

随机推荐

  1. rails generator

    generate 查找顺序 rails/generators/initializer/initializer_generator.rb generators/initializer/initializ ...

  2. input propertychange(1)

    input type=“text” 通过js改变输入框的value值是不会出发input propertychange事件

  3. OC知识点(类方法,构造方法,组合模式,get,set方法,自动生成属性)

    1.类方法的优势 不用创建对象,节省了空间,直接用类名调用类方法,类方法为外界提供一个方便的调用接口.(特点:类方法以加号开头,不能使用自身的成员变量,它的调用不依赖成员变量) 2.构造方法(初始化成 ...

  4. 0801 RESTAPI设计,DRF 序列化

    1.内容回顾    1.restframework serializer(序列化)的简单使用                QuereySet([obj,obj,obj])  -->  JSON ...

  5. Shell编程之Expect自动化交互程序

    一.Expect自动化交互程序 1.spawn命令 通过spawn执行一个命令或程序,之后所有的Expect操作都会在这个执行过的命令或程序进程中进行,包括自动交互功能. 语法: spawn [ 选项 ...

  6. Python mysql表数据和json格式的相互转换

    功能: 1.Python 脚本将mysql表数据转换成json格式 2.Python 脚本将json数据转成SQL插入数据库 表数据: SQL查询:SELECT id,NAME,LOCAL,mobil ...

  7. each方法的简单使用

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/stric ...

  8. java 学习汇总

    一.安装svn 插件 1.svn - http://subclipse.tigris.org/update 安装的过程中可能出错,解决方法: 导航到:The Eclipse Project Updat ...

  9. Navicat 导入Excel与增加主键

    1.当你需要导入某Excel文件时,你必须把这个lxsl文件用Excel先打开(与其他软件的导入有点不太,其他会报错已占用之类的) 2.设置主键 当你打开你导入的Excel文件时,会显示无主键,需要你 ...

  10. hibernate.cfg.xml_属性"connection.url"_指定字符集

    1.Oracle 2.MySQL 3. 4. 5.