Skip to content

Commit 90e73f6

Browse files
committedDec 27, 2020
feat: 2-keys-keyboard
1 parent 243dd25 commit 90e73f6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
 

Diff for: ‎dp.2-keys-keyboard.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
"""
3+
650. 只有两个键的键盘
4+
https://leetcode-cn.com/problems/2-keys-keyboard/
5+
最初在一个记事本上只有一个字符 'A'。你每次可以对这个记事本进行两种操作:
6+
Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的)。
7+
Paste (粘贴) : 你可以粘贴你上一次复制的字符。
8+
给定一个数字 n 。你需要使用最少的操作次数,在记事本中打印出恰好 n 个 'A'。输出能够打印出 n 个 'A' 的最少操作次数。
9+
"""
10+
def minSteps(self, n: int) -> int:
11+
# 如果是质数,则是他本身
12+
# 如果是合数,则是分解到所有不能再分解的质数的操作次数的和
13+
res = 0
14+
for i in range(2, n + 1):
15+
while n % i == 0:
16+
res += i
17+
n = n // i
18+
19+
return res
20+
21+
22+
so = Solution()
23+
# 3
24+
print(so.minSteps(21))

0 commit comments

Comments
 (0)