LeetCode 650 2 Keys Keyboard (dp 推荐)

244 篇文章 0 订阅
177 篇文章 0 订阅

Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:

  1. Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).
  2. Paste: You can paste the characters which are copied last time.

Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'.

Example 1:

Input: 3
Output: 3
Explanation:
Intitally, we have one character 'A'.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get 'AA'.
In step 3, we use Paste operation to get 'AAA'.

Note:

  1. The n will be in the range [1, 1000].

题目链接:https://leetcode.com/problems/2-keys-keyboard/

题目分析:先无脑dp,dp[i][j]表示当前有i个'A'且粘贴板上有j个'A'时的最小操作次数,易得转移方程:

1)dp[i][j] = Math.min(dp[i][j], dp[i - j][j] + 1)   // 复制
2)dp[i][i] = Math.min(dp[i][i], dp[i][j] + 1)       // 粘贴

答案为max(dp[n][j])

43ms,时间击败8.95%

class Solution {
    public int minSteps(int n) {
        int[][] dp = new int[n + 1][n + 1];
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= n; j++) {
                dp[i][j] = n + 1;
            }
        }
        dp[0][0] = 0;
        dp[1][0] = 0;
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= i; j++) {
                dp[i][j] = Math.min(dp[i][j], dp[i - j][j] + 1);
                dp[i][i] = Math.min(dp[i][i], dp[i][j] + 1);
            }
        }
        int ans = n + 1;
        for (int i = 0; i <= n; i++) {
            ans = Math.min(ans, dp[n][i]);
        }
        return ans;
    }
}

考虑优化,从例子中挖掘性质,假设n等于12,设dp[i]表示有i个'A'时的最小操作,c表示copy,p表示paste,则能得到12的操作有

c + 11p                 =>    dp[0] + n = n
c + p + c + 5p       =>    dp[2] + 1 + (n - 2) / 2 = dp[2] + n / 2
c + 2p + c + 3p     =>    dp[3] + 1 + (n - 3) / 3 = dp[3] + n / 3
c + 3p + c + 2p     =>    dp[4] + 1 + (n - 4) / 4 = dp[4] + n / 4

因为copy只能是整串,所以必须是n的约数才能copy,否则之后凑不出n

dp[i] = min( dp[j] + i / j (i % j == 0) )

19ms,时间击败35.71%

class Solution {
    public int minSteps(int n) {
        int[] dp = new int[n + 1];
        for (int i = 2; i <= n; i++) {
            dp[i] = i;
            for (int j = i >> 1; j > 1; j --) {
                if (i % j == 0) {
                    dp[i] = Math.min(dp[i], dp[j] + i / j);
                }
            }
        }
        return dp[n];
    }
}

容易发现内层循环存在大量无效判断,考虑直接枚举约数,类似nlogn的素数筛

2ms,时间击败83.75%

class Solution {
    public int minSteps(int n) {
        int[] dp = new int[n + 1];
        for (int i = 2; i <= n; i++) {
            dp[i] = i;
        }
        for (int i = 2; i <= n; i++) {
            for (int j = i << 1; j <= n; j += i) {
                dp[j] = Math.min(dp[j], dp[i] + j / i);
            }
        }
        return dp[n];
    }
}

算术基本定理

0ms,时间击败100%

class Solution {
    public int minSteps(int n) {
        int ans = 0, p = 2;
        while (n > 1) {
            while (n % p == 0) {
                ans += p;
                n /= p;
            }
            p++;
        }
        return ans;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值