1. 程式人生 > >[Swift]LeetCode279. 完全平方數 | Perfect Squares

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

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 }