2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:
Copy All
: You can copy all the characters present on the notepad (partial copy is not allowed).
Paste
: You can paste the characters which are copied
last time
.
Given a numbern
. You have to getexactlyn
'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to getn
'A'.
Example 1:
Note:
The
n
will be in the range [1, 1000].
分析
DP:想象每次8从4copy+paste来,就是额外增加2次操作。
dp[8] = dp[4]+8/4
https://leetcode.com/problems/2-keys-keyboard/discuss/105899/Java-DP-Solution
贪心:每次一个数可以被N整除就加上那个数,不行再换下一个,res就是所有可以被整除的数的总和。
Last updated