Skip to content
This repository was archived by the owner on Apr 27, 2025. It is now read-only.

Files

Latest commit

a071bb2 · May 9, 2022

History

History
72 lines (63 loc) · 1.41 KB

279-Perfect-Squares.md

File metadata and controls

72 lines (63 loc) · 1.41 KB

100. Perfect Squares

2020-07-30

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.

Solution

class Solution {
    
    var cache = [Int: Int]()
    var perfectSquare = [Int]()
    
    func findSum(count: Int, num: Int) -> Bool {
        switch count {
        case 0:
            return false
        case 1:
            guard let c = cache[num] else {
                return false
            }
            return c == 1
        default:
            if cache[num] != nil {
                return true
            }
            for sn in perfectSquare {
                if findSum(count: count - 1, num: num - sn) {
                    cache[num] = count
                    return true
                }
            }
            return false
        }
    }
    
    func numSquares(_ n: Int) -> Int {
        guard n > 0 else {
            return 0
        }
        for i in 1...n {
            let sq = i * i
            guard sq <= n else {
                break
            }
            cache[sq] = 1
            perfectSquare.append(sq)
        }
        for i in 1...n {
            if findSum(count: i, num: n) {
                return i
            }
        }
        return n
    }
}