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

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 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10485462.html