返回介绍

solution / 0600-0699 / 0651.4 Keys Keyboard / README_EN

发布于 2024-06-17 01:03:35 字数 2779 浏览 0 评论 0 收藏 0

651. 4 Keys Keyboard

中文文档

Description

Imagine you have a special keyboard with the following keys:

  • A: Print one 'A' on the screen.
  • Ctrl-A: Select the whole screen.
  • Ctrl-C: Copy selection to buffer.
  • Ctrl-V: Print buffer on screen appending it after what has already been printed.

Given an integer n, return _the maximum number of _'A'_ you can print on the screen with at most _n_ presses on the keys_.

 

Example 1:

Input: n = 3
Output: 3
Explanation: We can at most get 3 A's on screen by pressing the 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

 

Constraints:

  • 1 <= n <= 50

Solutions

Solution 1

class Solution:
  def maxA(self, n: int) -> int:
    dp = list(range(n + 1))
    for i in range(3, n + 1):
      for j in range(2, i - 1):
        dp[i] = max(dp[i], dp[j - 1] * (i - j))
    return dp[-1]
class Solution {
  public int maxA(int n) {
    int[] dp = new int[n + 1];
    for (int i = 0; i < n + 1; ++i) {
      dp[i] = i;
    }
    for (int i = 3; i < n + 1; ++i) {
      for (int j = 2; j < i - 1; ++j) {
        dp[i] = Math.max(dp[i], dp[j - 1] * (i - j));
      }
    }
    return dp[n];
  }
}
class Solution {
public:
  int maxA(int n) {
    vector<int> dp(n + 1);
    iota(dp.begin(), dp.end(), 0);
    for (int i = 3; i < n + 1; ++i) {
      for (int j = 2; j < i - 1; ++j) {
        dp[i] = max(dp[i], dp[j - 1] * (i - j));
      }
    }
    return dp[n];
  }
};
func maxA(n int) int {
  dp := make([]int, n+1)
  for i := range dp {
    dp[i] = i
  }
  for i := 3; i < n+1; i++ {
    for j := 2; j < i-1; j++ {
      dp[i] = max(dp[i], dp[j-1]*(i-j))
    }
  }
  return dp[n]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文