Skip to content

Latest commit

 

History

History
78 lines (69 loc) · 1.84 KB

File metadata and controls

78 lines (69 loc) · 1.84 KB

279. 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

Solutions (Rust)

1. BFS

use std::collections::HashSet;

impl Solution {
    pub fn num_squares(n: i32) -> i32 {
        let pt_sq: Vec<i32> = (1..=n).map(|x| x * x).filter(|&x| x <= n).collect();
        let mut set = HashSet::new();
        let mut v = vec![n];
        let mut cnt = 1;
 
        while !v.is_empty() {
            for _ in 0..v.len() {
                let num = v.remove(0);
                for i in &pt_sq {
                    if num - i == 0 {
                        return cnt;
                    } else if num - i > 0 && !set.contains(&(num - i)) {
                        set.insert(num - i);
                        v.push(num - i);
                    } else if num - i < 0 {
                        break;
                    }
                }
            }
            cnt += 1;
        }
        0
    }
}

2. Dynamic Programming

impl Solution {
    pub fn num_squares(n: i32) -> i32 {
        let n = n as usize;
        let mut v = vec![0; n];
 
        let mut i = 1;
        while i * i <= n {
            v[i * i - 1] = 1;
            i += 1;
        }
 
        for i in 2..=n {
            if v[i - 1] == 0 {
                v[i - 1] = i as i32;
                let mut j = 1;
                while j * j < i {
                    v[i - 1] = v[i - 1].min(v[i - j * j - 1] + 1);
                    j += 1;
                }
            }
        }
        v[n - 1]
    }
}