Guest User

Untitled

a guest
Feb 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.32 KB | None | 0 0
  1. class Solution {
  2. public int numSquares(int n) {
  3. int[] dp = new int[n + 1];
  4. Arrays.fill(dp, Integer.MAX_VALUE);
  5. for (int i = 1; i * i <= n; i++) {
  6. dp[i * i] = 1;
  7. }
  8. for (int i = 2; i <= n; i++) {
  9. for (int j = 1; j <= i / 2; j++) {
  10. dp[i] = Integer.min(dp[i], dp[j] + dp[i - j]);
  11. }
  12. }
  13. return dp[n];
  14. }
  15. }
Add Comment
Please, Sign In to add comment