LeetCode/rs/perfect-squares.rs
Matej Focko b562aea45e
rs: add «279. Perfect Squares»
Signed-off-by: Matej Focko <mfocko@redhat.com>
2024-02-08 10:24:08 +01:00

65 lines
1.2 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use std::cmp;
// We are guaranteed n ∈ ⟨1 ; 10⁴⟩
const UPPER_BOUND: i32 = 2000000000;
// Flag for unknown values
const UNKNOWN: i32 = -1;
struct Solver {
dp: Vec<i32>,
}
impl Solver {
fn new(n: i32) -> Self {
let mut dp = vec![UNKNOWN; (n as usize) + 1];
dp[0] = 0;
Self { dp }
}
fn solve(&mut self, n: i32) -> i32 {
if n < 0 {
return UPPER_BOUND;
}
let idx = n as usize;
if self.dp[idx] != UNKNOWN {
return self.dp[idx];
}
self.dp[idx] = UPPER_BOUND;
for i in (1..=n).take_while(|i| i * i <= n) {
self.dp[idx] = cmp::min(self.dp[idx], 1 + self.solve(n - i * i));
}
self.dp[idx]
}
}
struct Solution {}
impl Solution {
pub fn num_squares(n: i32) -> i32 {
Solver::new(n).solve(n)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1() {
assert_eq!(Solution::num_squares(12), 3);
}
#[test]
fn test_2() {
assert_eq!(Solution::num_squares(13), 2);
}
#[test]
fn test_3() {
assert_eq!(Solution::num_squares(55), 4);
}
}