[leetcode-651-4 Keys Keyboard]

Imagine you have a special keyboard with the following keys:

Key 1: (A): Prints 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.

思路:

要想N步生成最多个A,可在N-2步的时候,Ctrl A,N-1步的时候,Ctrl C,第N步的时候Ctrl V,这样就能将N-3步生成的A的个数,翻倍。

如何确定在第几步Ctrl A,然后再Ctrl C、Ctrl V呢,需要依次判断第i-3步之前的步骤。

得到递推公式 dp[i] = max(dp[i],dp[i-j-1]);dp[i]表示第i步生成的最多的A的个数。

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

https://discuss.leetcode.com/topic/97628/java-4-lines-recursion-with-step-by-step-explanation-to-derive-dp

时间: 2024-10-17 06:40:29

[leetcode-651-4 Keys Keyboard]的相关文章

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

[抄题]: 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 wh

[LeetCode] 4 Keys Keyboard 四键的键盘

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

[LeetCode] 2 Keys Keyboard 两键的键盘

Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). Paste: You can paste the character

Leetcode刷题总结:650. 2 Keys Keyboard

题目: 求获取n个A的最小操作步骤的数目minStep Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: Copy All: 只能全部复制,不能部分复制. Paste: 拷贝留在剪贴板上的字符 Given a number n. You have to get exactly n 'A' on the not

[leetcode-650-2 Keys Keyboard]

Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). Paste: You can paste the character

650. 2 Keys Keyboard

public int minSteps(int n) { /* 由于只能复制全部或者一个,所以: 如果一个数不是质数,那么最快的就是由最大的公因子复制而来 如果一个数是质数,那么所有方式都大于等于全部由1复制而来 网上看了看这个规律都是观察得出的,所以直接背过: 两个键的键盘问题(只能全部复制或者粘贴问题): 非质数就考虑最大公因子,质数就直接返回n(当然考察质数也是需要循环判断没有因子) */ if (n <= 1) return 0; int[] dp = new int[n+1]; dp[

leetcode 500. 键盘行(Keyboard Row)

目录 题目描述: 示例: 解法: 题目描述: 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例: 输入: ["Hello", "Alaska", "Dad", "Peace"] 输出: ["Alaska", "Dad"] 注意: 你可以重复使用键盘上同一字符. 你可以假设输入的字符串将只包含字母. 解法: class Solution { publ

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482