[Swift]LeetCode279. 徹底平方數 | Perfect Squares

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

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.git

Example 1:github

Input: n = 
Output: 3 
Explanation: 1212 = 4 + 4 + 4.

Example 2:微信

Input: n = 
Output: 2
Explanation: 1313 = 4 + 9.

給定正整數 n,找到若干個徹底平方數(好比 1, 4, 9, 16, ...)使得它們的和等於 n。你須要讓組成和的徹底平方數的個數最少。spa

示例 1:code

輸入: n = 
輸出: 3 
解釋: 1212 = 4 + 4 + 4.

示例 2:htm

輸入: n = 
輸出: 2
解釋: 1313 = 4 + 9.

12ms
 1 class Solution {
 2     func numSquares(_ n: Int) -> Int {
 3         var n = n
 4         while n % 4 == 0 {
 5             n /= 4
 6         }
 7         
 8         if n % 8 == 7 {
 9             return 4
10         }
11         
12         var a = 0
13         while a * a <= n {
14             let b = Int(sqrt(Double(n - a * a)))
15             if a * a + b * b == n {
16                 let ca =  a == 0 ? 0 : 1
17                 let cb = b == 0 ? 0 : 1
18                 return ca + cb
19             }
20             a += 1
21         }
22         
23         return 3
24     }
25 }

1236msblog

 1 class Solution {
 2     func numSquares(_ n: Int) -> Int {
 3         var dp: [Int] = [Int](repeating: Int.max, count: n+1)
 4         dp[0] = 0
 5         for i in 0...n {
 6             var j = 1
 7             while i + j * j <= n {
 8                 dp[i + j * j] = min(dp[i] + 1, dp[i + j * j])
 9                 j += 1
10             }
11         }
12         
13         return dp[n]
14     }
15 }

1308msget

 1 class Solution {
 2     func numSquares(_ n: Int) -> Int {
 3         
 4         var dp = [Int](repeating: 9223372036854775807, count: n+1)
 5         dp[0] = 0
 6         for i in 0...n {
 7             var j = 1
 8             while i + j*j <= n {
 9                 dp[i+j*j] = min(dp[i+j*j], dp[i]+1)
10                 j += 1
11             }
12         }
13         return dp.last ?? 1
14         
15     }
16 }

1324ms博客

 1 class Solution {
 2 func numSquares(_ n: Int) -> Int {
 3     guard n > 0 else {
 4         return 0
 5     }
 6     
 7     var dp = Array.init(repeating: Int.max, count: n + 1)
 8     dp[0] = 0
 9     
10     for i in 0...n {
11         var j = 1
12         while i + j * j <= n {
13             dp[i + j * j] = min(dp[i + j * j], dp[i] + 1)
14             
15             j += 1
16         }
17     }
18     
19     return dp[n]
20 }
21 }

1484ms

 1 class Solution {
 2     func numSquares(_ n: Int) -> Int {
 3         var nums = Array(repeating: Int.max, count: n + 1)
 4         nums[0] = 0
 5         for index in 0...n {
 6             var j = 1
 7             while index + j * j <= n {
 8                 nums[index + j * j] = min(nums[index + j * j], nums[index] + 1)
 9                 j += 1
10             }
11         }
12         return nums.last!
13     }
14 }

1596ms

 1 class Solution {
 2     var times: [Int] = []
 3     var once: [Int] = []
 4     var candidates: Set<Int> = Set<Int>()
 5     
 6     func createArrs(_ n: Int) {
 7         times = [Int](repeating:0, count:n+1)
 8         for i in 1...n {
 9             times[i] = i
10         }
11 
12         var cnt = Int(sqrt(Double(n)))
13         once = [Int](repeating:0, count:cnt+1)
14         for i in 1...cnt {
15             times[i*i] = 1
16             once[i] = i*i
17         }
18         
19         let once_set = Set(once)
20         for i in 1...n {
21             candidates.insert(i)
22         }
23     }
24     
25     func fillTheTimes(_ curTimes: Int) {
26         var preTimes = curTimes - 1
27         for i in 1...times.count - 1 {
28             if times[i] == preTimes {
29                 for j in 1...once.count - 1 {
30                     if once[j] > i {
31                         break
32                     }
33                     var k = i + once[j]
34                     if k > times.count - 1 {
35                         break
36                     }
37                     if times[k] > curTimes {
38                         times[k] = curTimes
39                     }
40                 }
41             }
42         }
43     }
44     
45     func fillAllTimes() {
46         var cur_times = 1
47         while times[times.count - 1] > cur_times {
48             cur_times += 1
49             fillTheTimes(cur_times)
50         }
51     }
52     
53     func f(_ n: Int) -> Int {
54         createArrs(n)
55         fillAllTimes()
56         return times[n]
57     }
58     
59     func numSquares(_ n: Int) -> Int {
60         return f(n)
61     }
62 }
相關文章
相關標籤/搜索