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

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

12
12 = 4 + 4 + 4.

Example 2:

13
13 = 4 + 9.

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

示例 1:

12
12 = 4 + 4 + 4.

示例 2:

13
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 @   为敢技术  阅读(418)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°