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.

C++:

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

C++:

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

  

类似题目:

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

All LeetCode Questions List 题目汇总

[LeetCode] 651. 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] 650. 2 Keys Keyboard 两键的键盘

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

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

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

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

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

  5. 【LeetCode】650. 只有两个键的键盘

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

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

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

  7. LeetCode 650 - 2 Keys Keyboard

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

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

随机推荐

  1. java基础(14)---修饰符

    修饰符:final .static.public.protected.private.default. 一.final(不能修改) 使用final修饰变量定义:该变量一旦被初始化之后就不允许再被修改. ...

  2. selenium与webdriver驱动与firefox、 chrome匹配版本

    一.Chrome python3 selenium3.11.0(直接pip安装即可) chromedriver_win32:2.38 Chrome版本: 65.0.3325.146(正式版本)(32 ...

  3. 删除线性表中所有值为x的元素

    时间复杂度O(n),空间复杂度O(1). 简单的问题两种不同的思路. 代码: #include <stdio.h> #define MAX 100 struct sqlist{ int d ...

  4. C# 验证控件的使用RequiredFieldValidator&CompareValidator

    使用验证控件可以向服务器提交表单数据时验证表单内容,下面以RequiredFieldValidator和CompareValidator为例说明验证控件的用法 RequiredFieldValidat ...

  5. hexo与github page搭建博客

    安装 npm i hexo-cli -g hexo init blog cd blog npm install hexo server 发布hexo到github page npm i hexo-de ...

  6. MySQL 中间件 - DBLE 简单使用

    DBLE 是企业级开源分布式中间件,江湖人送外号 “MyCat Plus”:以其简单稳定,持续维护,良好的社区环境和广大的群众基础得到了社区的大力支持: 环境准备 DBLE项目资料   DBLE官方网 ...

  7. asp.net web开发——文件的上传和下载

    HTML部分 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.a ...

  8. 查看.NET应用程序中的异常(下)

    为什么要使用内存转储进行调试? 在两种主要情况下,您可能需要使用内存转储进行调试.第一种情况是应用程序有一个未处理的异常并崩溃,而您只有一个内存转储.第二种情况是,在生产环境中出现异常或特定行为,并且 ...

  9. mysql 查询账户

    查询 mysql 的存在的账户  >select user,host,password from mysql.user; # 可以查询涉及到user. host 链接权限.密码加密文件.

  10. mysql mod() 获取余数

    mysql> ,); +-----------+ | mod(,) | +-----------+ | | +-----------+ row in set (0.00 sec)