LeetCode 4 Keys Keyboard
原题链接在这里: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 <= N <= 50
- 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];
}
}
LeetCode 4 Keys Keyboard的更多相关文章
- [LeetCode] 4 Keys Keyboard 四键的键盘
Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...
- [LeetCode] 2 Keys Keyboard 两键的键盘
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- Leetcode 之 Keys Keyboard
1. 2 Keys Keyboard 先把dp的最小不走都设置为无穷大(Integer.MAX_VALUE),初始化条件:dp[0] = dp[1] = 0,状态转移方程为dp[i] = Math.m ...
- 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 ...
- [LeetCode] 650. 2 Keys Keyboard 两键的键盘
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- [LeetCode] 651. 4 Keys Keyboard 四键的键盘
Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...
- [leetcode] 650. 2 Keys Keyboard (Medium)
解法一: 暴力DFS搜索,对每一步进行复制还是粘贴的状态进行遍历. 注意剪枝的地方: 1.当前A数量大于目标数量,停止搜索 2.当前剪贴板数字大于等于A数量时,只搜索下一步为粘贴的状态. Runtim ...
- LC 650. 2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- LeetCode 650 - 2 Keys Keyboard
LeetCode 第650题 Initially on a notepad only one character 'A' is present. You can perform two operati ...
随机推荐
- 每天一个Linux命令(47)route命令
Linux系统的route命令用于显示和操作内核IP路由表(show / manipulate the IP routing table). (1)用法: 用法: route ...
- LeetCode: Keyboard Row
代码长了些,但还是比较简单的 public class Solution { public String[] findWords(String[] words) { List<String> ...
- Django-虚拟环境设置
Django 虚拟环境virtualenv virtualenv是用来处理多个用python语言进行开发的项目,在同一台机器上部署,不同项目依赖不同第三方库版本所造成的问题. 打个比方,现在你机器上要 ...
- Scrapy安装方法
Scrapy安装在Python2.7环境下 1.配置环境变量: 2.安装基础软件 4个(64位系统) 安装twisted: C:\Users\Administrator>pip install ...
- 默认连接电脑的模式为MTP【转】
本文转载自:https://blog.csdn.net/tangzhihai0421/article/details/53487208 Android L后默认的usb连接模式为“仅充电”,而且不会随 ...
- C语言中static的使用方法【转】
本文转自:http://blog.csdn.net/renren900207/article/details/21609649 全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量 ...
- Java Map增删改查
示例代码: 学生类 package com.imooc.collection; import java.util.HashSet; import java.util.Set; /** * 学生类 * ...
- Kubernetes RBAC
在Kubernetes1.6版本中新增角色访问控制机制(Role-Based Access,RBAC)让集群管理员可以针对特定使用者或服务账号的角色,进行更精确的资源访问控制.在RBAC中,权限与角色 ...
- Elasticsearch安装笔记
下载安装包 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.2.zip 开始执行bin/./el ...
- 初探MyBatis之HelloWorld(三)
三.用SQL映射语句用注解,dataSource用xml(不推荐). 综合上面两节(一个用xml,一个用annotation),发现一个好玩儿的,SQL映射用注解方式,然后还是得有两个xml配置文件. ...