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:

  1. Input: N = 3
  2. Output: 3
  3. Explanation:
  4. We can at most get 3 A's on screen by pressing following key sequence:
  5. A, A, A

Example 2:

  1. Input: N = 7
  2. Output: 9
  3. Explanation:
  4. We can at most get 9 A's on screen by pressing following key sequence:
  5. 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++:

  1. class Solution {
  2. public:
  3. int maxA(int N) {
  4. int res = N;
  5. for (int i = 1; i < N - 2; ++i) {
  6. res = max(res, maxA(i) * (N - 1 - i));
  7. }
  8. return res;
  9. }
  10. };

C++:

  1. class Solution {
  2. public:
  3. int maxA(int N) {
  4. vector<int> dp(N + 1, 0);
  5. for (int i = 0; i <= N; ++i) {
  6. dp[i] = i;
  7. for (int j = 1; j < i - 2; ++j) {
  8. dp[i] = max(dp[i], dp[j] * (i - j - 1));
  9. }
  10. }
  11. return dp[N];
  12. }
  13. };

  

类似题目:

[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. js插件---videojs中文文档详解

    js插件---videojs中文文档详解 一.总结 一句话总结: js插件网上都有很多参考资料,使用起来也非常简单 二.lavarel中使用实例 <video id="example_ ...

  2. Centos7 Rsync怎么实现热备份笔记

    应用场景:备份Web服务器相关目录下的数据文件,确保指定目录下的所有文件同步. 操作系统:Centos 7 服务器两台:web服务器 172.19.242.70  备份服务器 172.19.242.7 ...

  3. Python开发AI应用-国际象棋应用

    AI 部分总述     AI在做出决策前经过三个不同的步骤.首先,他找到所有规则允许的棋步(通常在开局时会有20-30种,随后会降低到几种).其次,它生成一个棋步树用来随后决定最佳决策.虽然树的大小随 ...

  4. Fiddler——手机端无法安装证书

    前言 一个APP测试,需要抓包,设置好代理后,访问代理地址,下载证书,下载完成却不能安装. 提示:无法读取该证书文件 手机型号: OPPO A5 步骤 设置->其他设置->设备与隐私-&g ...

  5. Ubuntu 14.04.2配置rsync

    ubuntu系统自带rsync,首先配置/etc/default/rsync,启用daemon模式 修改/etc/rsyncd.conf配置文件 cat /etc/rsyncd.conf # samp ...

  6. dbms_lob包学习笔记之三:instr和substr存储过程

    instr和substr存储过程,分析内部大对象的内容 instr函数与substr函数 instr函数用于从指定的位置开始,从大型对象中查找第N个与模式匹配的字符串. 用于查找内部大对象中的字符串的 ...

  7. java项目部署

    本文章只为帮助大家学习项目的发布,为基础篇,在此给大家示范在window环境下的项目部署及运维. 以下版本为讲解示例,可自行改至匹配版本. 服务器:window service2008 R2 Stan ...

  8. Expectation Maximization Algorithm

    期望最大化算法EM. 简介 EM算法即期望最大化算法,由Dempster等人在1976年提出[1].这是一种迭代法,用于求解含有隐变量的最大似然估计.最大后验概率估计问题.至于什么是隐变量,在后面会详 ...

  9. php Web 项目的文件/文件夹上传下载

    PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...

  10. IDEA的foreach循环

    试了试其他快捷键, 突然发现的... 先弄一个list 再把变量名写出来先 按快捷键 ctrl+alt+J, 选最后一个 看效果