返回介绍

solution / 0200-0299 / 0279.Perfect Squares / README_EN

发布于 2024-06-17 01:04:02 字数 6222 浏览 0 评论 0 收藏 0

279. Perfect Squares

中文文档

Description

Given an integer n, return _the least number of perfect square numbers that sum to_ n.

A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1, 4, 9, and 16 are perfect squares while 3 and 11 are not.

 

Example 1:

Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.

Example 2:

Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.

 

Constraints:

  • 1 <= n <= 104

Solutions

Solution 1

class Solution:
  def numSquares(self, n: int) -> int:
    m = int(sqrt(n))
    f = [[inf] * (n + 1) for _ in range(m + 1)]
    f[0][0] = 0
    for i in range(1, m + 1):
      for j in range(n + 1):
        f[i][j] = f[i - 1][j]
        if j >= i * i:
          f[i][j] = min(f[i][j], f[i][j - i * i] + 1)
    return f[m][n]
class Solution {
  public int numSquares(int n) {
    int m = (int) Math.sqrt(n);
    int[][] f = new int[m + 1][n + 1];
    for (var g : f) {
      Arrays.fill(g, 1 << 30);
    }
    f[0][0] = 0;
    for (int i = 1; i <= m; ++i) {
      for (int j = 0; j <= n; ++j) {
        f[i][j] = f[i - 1][j];
        if (j >= i * i) {
          f[i][j] = Math.min(f[i][j], f[i][j - i * i] + 1);
        }
      }
    }
    return f[m][n];
  }
}
class Solution {
public:
  int numSquares(int n) {
    int m = sqrt(n);
    int f[m + 1][n + 1];
    memset(f, 0x3f, sizeof(f));
    f[0][0] = 0;
    for (int i = 1; i <= m; ++i) {
      for (int j = 0; j <= n; ++j) {
        f[i][j] = f[i - 1][j];
        if (j >= i * i) {
          f[i][j] = min(f[i][j], f[i][j - i * i] + 1);
        }
      }
    }
    return f[m][n];
  }
};
func numSquares(n int) int {
  m := int(math.Sqrt(float64(n)))
  f := make([][]int, m+1)
  const inf = 1 << 30
  for i := range f {
    f[i] = make([]int, n+1)
    for j := range f[i] {
      f[i][j] = inf
    }
  }
  f[0][0] = 0
  for i := 1; i <= m; i++ {
    for j := 0; j <= n; j++ {
      f[i][j] = f[i-1][j]
      if j >= i*i {
        f[i][j] = min(f[i][j], f[i][j-i*i]+1)
      }
    }
  }
  return f[m][n]
}
function numSquares(n: number): number {
  const m = Math.floor(Math.sqrt(n));
  const f: number[][] = Array(m + 1)
    .fill(0)
    .map(() => Array(n + 1).fill(1 << 30));
  f[0][0] = 0;
  for (let i = 1; i <= m; ++i) {
    for (let j = 0; j <= n; ++j) {
      f[i][j] = f[i - 1][j];
      if (j >= i * i) {
        f[i][j] = Math.min(f[i][j], f[i][j - i * i] + 1);
      }
    }
  }
  return f[m][n];
}
impl Solution {
  pub fn num_squares(n: i32) -> i32 {
    let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize);
    let mut dp = vec![vec![i32::MAX; col + 1]; row + 1];
    dp[0][0] = 0;
    for i in 1..=row {
      for j in 0..=col {
        dp[i][j] = dp[i - 1][j];
        if j >= i * i {
          dp[i][j] = std::cmp::min(dp[i][j], dp[i][j - i * i] + 1);
        }
      }
    }
    dp[row][col]
  }
}

Solution 2

class Solution:
  def numSquares(self, n: int) -> int:
    m = int(sqrt(n))
    f = [0] + [inf] * n
    for i in range(1, m + 1):
      for j in range(i * i, n + 1):
        f[j] = min(f[j], f[j - i * i] + 1)
    return f[n]
class Solution {
  public int numSquares(int n) {
    int m = (int) Math.sqrt(n);
    int[] f = new int[n + 1];
    Arrays.fill(f, 1 << 30);
    f[0] = 0;
    for (int i = 1; i <= m; ++i) {
      for (int j = i * i; j <= n; ++j) {
        f[j] = Math.min(f[j], f[j - i * i] + 1);
      }
    }
    return f[n];
  }
}
class Solution {
public:
  int numSquares(int n) {
    int m = sqrt(n);
    int f[n + 1];
    memset(f, 0x3f, sizeof(f));
    f[0] = 0;
    for (int i = 1; i <= m; ++i) {
      for (int j = i * i; j <= n; ++j) {
        f[j] = min(f[j], f[j - i * i] + 1);
      }
    }
    return f[n];
  }
};
func numSquares(n int) int {
  m := int(math.Sqrt(float64(n)))
  f := make([]int, n+1)
  for i := range f {
    f[i] = 1 << 30
  }
  f[0] = 0
  for i := 1; i <= m; i++ {
    for j := i * i; j <= n; j++ {
      f[j] = min(f[j], f[j-i*i]+1)
    }
  }
  return f[n]
}
function numSquares(n: number): number {
  const m = Math.floor(Math.sqrt(n));
  const f: number[] = Array(n + 1).fill(1 << 30);
  f[0] = 0;
  for (let i = 1; i <= m; ++i) {
    for (let j = i * i; j <= n; ++j) {
      f[j] = Math.min(f[j], f[j - i * i] + 1);
    }
  }
  return f[n];
}
impl Solution {
  pub fn num_squares(n: i32) -> i32 {
    let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize);
    let mut dp = vec![i32::MAX; col + 1];
    dp[0] = 0;
    for i in 1..=row {
      for j in i * i..=col {
        dp[j] = std::cmp::min(dp[j], dp[j - i * i] + 1);
      }
    }
    dp[col]
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文