Re: [闲聊] 每日leetcode

楼主: dont   2024-08-19 13:57:07
650. 2 Keys Keyboard
## 思路
两层for-loop更新DP
dp[j] = min(dp[j], dp[i] + step)
step = copy&paste次数
## Code
```python
class Solution:
def minSteps(self, n: int) -> int:
if n == 1:
return 0
dp = list(range(1+n))
for i in range(2, 1 + n//2):
step = 1 # copy
for j in range(i+i, 1+n, i):
step += 1 # paste
dp[j] = min(dp[j], dp[i] + step)
return dp[-1]
```
作者: sustainer123 (caster)   2024-08-19 13:58:00
大师
作者: DJYOMIYAHINA (通通打死)   2024-08-19 14:16:00
大湿

Links booklink

Contact Us: admin [ a t ] ucptt.com