【Leetcode】650. 2 Keys Keyboard

    科技2022-08-05  95

    题目地址:

    https://leetcode.com/problems/2-keys-keyboard/

    有一个笔记本,上面写了一个字符 A A A,每次可以执行下面两个操作之一: 1、复制笔记本上已经存在的所有字符; 2、粘贴上一次复制过的字符。 问要让笔记本上恰好出现 n n n A A A至少需要多少次操作。

    详细解释和数学证明可以参考https://blog.csdn.net/qq_46105170/article/details/108925312。代码如下:

    public class Solution { public int minSteps(int n) { int res = 0; for (int i = 2; i <= n; i++) { while (n % i == 0) { res += i; n /= i; } } return res; } }

    n n n的素因子分解是 p 1 s 1 . . . p k s k p_1^{s_1}...p_k^{s_k} p1s1...pksk,则时间复杂度 O ( ∑ s i ) O(\sum s_i) O(si),空间 O ( 1 ) O(1) O(1)

    Processed: 0.015, SQL: 8