两个键的键盘
难度:
标签:
题目描述
代码结果
运行时间: 31 ms, 内存: 16.0 MB
/*
* Problem Statement: Starting with a single character 'A', you can perform two operations: 'Copy All' (copies all the characters in the notepad) and 'Paste' (pastes the last copied characters). Given a number 'n', determine the minimum number of operations required to produce exactly 'n' characters.
*
* Solution Approach: Similar to the Java approach, we use a stream to iterate over the possible factors and sum them up when the number 'n' can be divided by these factors. This approach also calculates the minimum steps by finding the prime factors of 'n'.
*/
import java.util.stream.IntStream;
public class Solution {
public int minSteps(int n) {
if (n == 1) return 0;
return IntStream.rangeClosed(2, n)
.filter(i -> n % i == 0)
.flatMap(i -> {
int[] arr = new int[n];
int count = 0;
while (n % i == 0) {
arr[count++] = i;
n /= i;
}
return IntStream.of(arr).limit(count);
})
.sum();
}
}
解释
方法:
这个题解采用的是素数分解的思路。对于任意一个大于1的整数n,可以将其分解为一系列素数的乘积。例如,12=2*2*3。题目要求的是打印出n个'A'所需的最少操作次数,实际上就等于n的素数分解式中所有素数的和。因为对于一个素数因子p,我们需要执行p次'Paste'操作和一次'Copy All'操作,总共p+1次操作。而n的素数分解的结果就是这些素数因子的集合,操作次数就是这些素数的和。题解中使用了从小到大枚举素数因子的方法,依次将n除以素数,同时累加操作次数,最后如果剩余的n大于1,则说明是一个大于sqrt(n)的素数因子,需要再加上n。
时间复杂度:
O(sqrt(n) * log n)
空间复杂度:
O(1)
代码细节讲解
🦆
题解中提到的`素数分解的思路`是如何与`Copy All`和`Paste`操作相对应的?能否详细解释一下这种映射关系?
▷🦆
为什么在素数分解方法中,对于每一个素数因子p,操作次数是p+1次而不是仅仅p次?
▷🦆
在计算过程中,当n大于2时直接将n加到结果中,这是基于什么样的假设?是否意味着n此时一定是素数?
▷🦆
题解只适用到了简单的变量进行计算,是否存在任何风险或缺陷,例如在特定情况下的效率问题或者潜在的错误?
▷相关问题
坏了的计算器
在显示着数字 startValue
的坏计算器上,我们可以执行以下两种操作:
- 双倍(Double):将显示屏上的数字乘 2;
- 递减(Decrement):将显示屏上的数字减
1
。
给定两个整数 startValue
和 target
。返回显示数字 target
所需的最小操作数。
示例 1:
输入:startValue = 2, target = 3 输出:2 解释:先进行双倍运算,然后再进行递减运算 {2 -> 4 -> 3}.
示例 2:
输入:startValue = 5, target = 8 输出:2 解释:先递减,再双倍 {5 -> 4 -> 8}.
示例 3:
输入:startValue = 3, target = 10 输出:3 解释:先双倍,然后递减,再双倍 {3 -> 6 -> 5 -> 10}.
提示:
1 <= startValue, target <= 109