[Swift]LeetCode650. 只有兩個鍵的鍵盤 | 2 Keys Keyboard

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-sfhdwelq-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

  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'.github

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:app

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

最初在一個記事本上只有一個字符 'A'。你每次能夠對這個記事本進行兩種操做:this

  1. Copy All (複製所有) : 你能夠複製這個記事本中的全部字符(部分的複製是不容許的)。
  2. Paste (粘貼) : 你能夠粘貼你上一次複製的字符。

給定一個數字 n 。你須要使用最少的操做次數,在記事本中打印出剛好 n 個 'A'。輸出可以打印出 n 個 'A' 的最少操做次數。spa

示例 1:code

輸入: 3
輸出: 3
解釋:
最初, 咱們只有一個字符 'A'。
第 1 步, 咱們使用 Copy All 操做。
第 2 步, 咱們使用 Paste 操做來得到 'AA'。
第 3 步, 咱們使用 Paste 操做來得到 'AAA'。

說明:orm

  1. n 的取值範圍是 [1, 1000] 。

4mshtm

 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 }
相關文章
相關標籤/搜索