敢教日月换新天。为有牺牲多壮志,

[Swift]LeetCode650. 只有两个键的键盘 | 2 Keys Keyboard

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10485462.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:

  1. Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).
  2. Paste: You can paste the characters which are copied last time. 

Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n'A'.

Example 1:

Input: 3
Output: 3
Explanation:
Intitally, we have one character 'A'.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get 'AA'.
In step 3, we use Paste operation to get 'AAA'. 

Note:

  1. The n will be in the range [1, 1000].

最初在一个记事本上只有一个字符 'A'。你每次可以对这个记事本进行两种操作:

  1. Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的)。
  2. Paste (粘贴) : 你可以粘贴你上一次复制的字符。

给定一个数字 n 。你需要使用最少的操作次数,在记事本中打印出恰好 n 个 'A'。输出能够打印出 n 个 'A' 的最少操作次数。

示例 1:

输入: 3
输出: 3
解释:
最初, 我们只有一个字符 'A'。
第 1 步, 我们使用 Copy All 操作。
第 2 步, 我们使用 Paste 操作来获得 'AA'。
第 3 步, 我们使用 Paste 操作来获得 'AAA'。

说明:

  1. n 的取值范围是 [1, 1000] 。

4ms

复制代码
 1 class Solution {
 2     func minSteps(_ n: Int) -> Int {
 3         if n == 1 {
 4             return 0
 5         }
 6         // n = p * q * r * .....
 7         // res = p + q + r + ....
 8         var mem = [Int: Int]()
 9         return getMin(n, &mem)
10     }
11     
12     func getMin(_ n: Int, _ mem: inout [Int: Int]) -> Int {
13         if let res = mem[n] {
14             return res
15         }
16         
17         let mid = Int(sqrt(Double(n)))
18         var res = n
19         var p = 2
20         while p <= mid {
21             if n % p == 0 {
22                 res = min(res, p + getMin(n/p, &mem))
23             }
24             p += 1
25         }
26         
27         return res
28     }
29 }
复制代码

8ms

复制代码
 1 class Solution {
 2     func minSteps(_ n: Int) -> Int {
 3         if n <= 1 {
 4             return 0
 5         }
 6         guard n > 3 else {
 7             return n
 8         }
 9         var res = 0
10         var num = n
11         var i = 2
12         while i < num + 1 {
13             while num % i == 0 {
14                 res += i
15                 num /= i
16             }
17             i += 1
18         }
19         return res
20     } 
21 }
复制代码

12ms

复制代码
 1 class Solution {
 2     func minSteps(_ n: Int) -> Int {        
 3         guard n > 1 else {
 4             return 0
 5         }
 6         var n = n        
 7         var result = 0        
 8         for i in 2 ... n {
 9             
10             while n % i == 0 {
11                 result += i
12                 n /= i
13             }
14         }        
15         return result
16     }
17 }
复制代码

24ms

复制代码
 1 class Solution {
 2     func minSteps(_ n: Int) -> Int {
 3         // n -> Cn
 4         // m -> n = Dm
 5         if n == 1{
 6             return 0
 7         }else if n < 4{
 8             return n
 9         }
10         
11         var MS = [Int](repeating: 0, count: n + 1)
12         MS[1] = 0
13         for i in 2...n{
14             MS[i] = i
15         }
16         var midX = n / 2
17         for i in 2...midX{
18             var multiplier = 2
19             while(i * multiplier <= n){
20                 MS[i * multiplier] = MS[i] + multiplier
21                 multiplier += 1
22             }   
23         }
24         return MS[n]
25     }
26 }
复制代码

24ms

复制代码
 1 class Solution {
 2     func minSteps(_ n: Int) -> Int {
 3     if(n==1){return 0}
 4     func isPrime(num:Int)->Bool{
 5         if(num==1){return true}
 6         for i in 1...Int(sqrt(Double.init(num))){
 7             if(i>1 && Double(num)/Double(i) - Double(num/i) == 0){return false}
 8         }
 9         return true
10     }
11     var n = n
12     var primeList = [Int]()
13     while !isPrime(num: n) {
14         var i = 2
15         while i <= Int(sqrt(Double.init(n))){
16             if(isPrime(num: i)){
17                 if  Double(n)/Double(i) - Double(n/i) == 0{
18                     primeList.append(i)
19                     n = n/i
20                     i=2
21                 }else{
22                     i+=1
23                 }
24             }else{
25                 i+=1
26                 
27             }
28         }
29     }
30     primeList.append(n)
31     return primeList.reduce(0) { (Result, mInt) -> Int in
32         return Result+mInt
33     }
34   }
35 }
复制代码

40ms

复制代码
 1 class Solution {
 2     func minSteps(_ n: Int) -> Int {
 3         if n == 1 {
 4             return 0
 5         }
 6         return minSteps(current: 1, n: n, copied: 1) + 1
 7     }    
 8     
 9     func minSteps(current: Int, n: Int, copied: Int) -> Int {
10         if current == n {
11             return 0
12         }
13         
14         if current > n {
15             return -1
16         }
17         
18         let copyCurrentAndPaste = minSteps(current: current * 2, n: n, copied: current)
19         let pasteAlreadyCopied = minSteps(current: current + copied, n: n, copied: copied)
20         
21         if copyCurrentAndPaste == -1 && pasteAlreadyCopied == -1 {
22             return -1
23         } else if copyCurrentAndPaste == -1 {
24             return pasteAlreadyCopied + 1
25         } else if pasteAlreadyCopied == -1 {
26             return copyCurrentAndPaste + 2
27         } else {
28             if copyCurrentAndPaste < pasteAlreadyCopied {
29                 return copyCurrentAndPaste + 2
30             } else {
31                 return pasteAlreadyCopied + 1
32             }
33         }
34         
35     }
36 }
复制代码

 

posted @   为敢技术  阅读(277)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°