LeetCode 650 2 Keys Keyboard (dp 推荐)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tc_To_Top/article/details/89666561

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;
    }
}

猜你喜欢

转载自blog.csdn.net/Tc_To_Top/article/details/89666561