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

[Swift]LeetCode279. 完全平方数 | Perfect Squares

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

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

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

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

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

Example 1:

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

Example 2:

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

给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。

示例 1:

输入: n = 12
输出: 3 
解释: 12 = 4 + 4 + 4.

示例 2:

输入: n = 13
输出: 2
解释: 13 = 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 }

1236ms

 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 }

1308ms

 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 }

posted @ 2019-01-08 20:11  为敢技术  阅读(412)  评论(0编辑  收藏  举报