原题链接在这里: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. 调用AJAX返回JSON、XML数据类型

    1.调用AJAX返回JSON数据 用下拉列表显示Nation表民族名称 主页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transi ...

  2. PAT 天梯赛 L1-006. 连续因子 【循环】

    题目链接 https://www.patest.cn/contests/gplt/L1-006 思路 输出的连续因子 的乘积 也要是这个数的因子 就每个数先找它的单因子 然后每个单因子往上一个一个遍历 ...

  3. C# 调用VS自带程序WebDev.WebServer40.EXE 源代码

    通过Process.Start启动,VS自带程序WebDev.WebServer40.EXE 在内网架设网站时,为安装IIS条件下用VS自带的小程序来测试效果非常不错! using System; u ...

  4. Mycat实现Mysql数据库读写分离

    Linux和Windows环境下搭建Mycat数据读写分离 前提需要:1.服务器装有JVM虚拟机,就是JDK.2.两个Mysql数据库已经实现主从复制,参考:https://www.cnblogs.c ...

  5. jni 编译错误error: unknown type name '__va_list'

     platforms\android-9\arch-arm\usr\include\stdio.h:257:37: error: unknown type name '__va_list'     解 ...

  6. RTC是DS1339,驱动采用的是rtc-ds1307.c

    我的外部RTC是DS1339,驱动采用的是rtc-ds1307.c在内核里选上了 <*> I2C support 以及 [*]   Set system time from RTC on  ...

  7. qt5.4.1的imx6编译

    2.到https://download.qt.io/archive/qt/5.4/5.4.1/single/下载源码包qt-everywhere-opensource-src-5.4.1.tar.gz ...

  8. jQuery可放大预览的图片滑块

    在线演示 本地下载

  9. start、run、join

    首先得了解什么是主线程,当Java程序启动时,一个线程立刻运行,该线程通常叫做程序的主线程(main thread).主线程的重要性体现在两方面:1. 它是产生其他子线程的线程:2. 通常它必须最后完 ...

  10. iOS_核心动画(二)

    目 录: 一.Core Animation开发步骤 二.Core Animation的继承结构 三.CAAnimation常用的属性 四.CAPropertyAnimation(属性动画) 五.CAB ...