|
| 1 | +package LeetCodeJava.DynamicProgramming; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/perfect-squares/description/ |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | + |
| 7 | +/** |
| 8 | + * 279. Perfect Squares |
| 9 | + * Solved |
| 10 | + * Medium |
| 11 | + * Topics |
| 12 | + * Companies |
| 13 | + * Given an integer n, return the least number of perfect square numbers that sum to n. |
| 14 | + * |
| 15 | + * 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. |
| 16 | + * |
| 17 | + * |
| 18 | + * |
| 19 | + * Example 1: |
| 20 | + * |
| 21 | + * Input: n = 12 |
| 22 | + * Output: 3 |
| 23 | + * Explanation: 12 = 4 + 4 + 4. |
| 24 | + * Example 2: |
| 25 | + * |
| 26 | + * Input: n = 13 |
| 27 | + * Output: 2 |
| 28 | + * Explanation: 13 = 4 + 9. |
| 29 | + * |
| 30 | + * |
| 31 | + * Constraints: |
| 32 | + * |
| 33 | + * 1 <= n <= 104 |
| 34 | + * |
| 35 | + */ |
| 36 | +public class PerfectSquares { |
| 37 | + |
| 38 | + // V0 |
| 39 | +// public int numSquares(int n) { |
| 40 | +// |
| 41 | +// } |
| 42 | + |
| 43 | + // V1 |
| 44 | + // IDEA: DP |
| 45 | + // https://leetcode.com/problems/perfect-squares/solutions/4694883/beats-99-users-cjavapythonjavascript-exp-37yg/ |
| 46 | + public int numSquares_1(int n) { |
| 47 | + int[] dp = new int[n + 1]; |
| 48 | + Arrays.fill(dp, Integer.MAX_VALUE); |
| 49 | + dp[0] = 0; |
| 50 | + for (int i = 1; i <= n; ++i) { |
| 51 | + int min_val = Integer.MAX_VALUE; |
| 52 | + for (int j = 1; j * j <= i; ++j) { |
| 53 | + min_val = Math.min(min_val, dp[i - j * j] + 1); |
| 54 | + } |
| 55 | + dp[i] = min_val; |
| 56 | + } |
| 57 | + return dp[n]; |
| 58 | + } |
| 59 | + |
| 60 | + // V2 |
| 61 | + // https://leetcode.com/problems/perfect-squares/solutions/1520447/dp-easy-to-understand-js-java-python-c-b-sk59/ |
| 62 | + // IDEA: DP |
| 63 | + public int numSquares_2(int n) { |
| 64 | + int[] dp = new int[n + 1]; |
| 65 | + Arrays.fill(dp, Integer.MAX_VALUE); |
| 66 | + dp[0] = 0; |
| 67 | + int count = 1; |
| 68 | + while (count * count <= n) { |
| 69 | + int sq = count * count; |
| 70 | + for (int i = sq; i <= n; i++) { |
| 71 | + dp[i] = Math.min(dp[i - sq] + 1, dp[i]); |
| 72 | + } |
| 73 | + count++; |
| 74 | + } |
| 75 | + return dp[n]; |
| 76 | + } |
| 77 | + |
| 78 | + // V3-1 |
| 79 | + // https://leetcode.com/problems/perfect-squares/solutions/2837992/java-recursion-memoization-dp-3-square-t-72qb/ |
| 80 | + // IDEA: Top Down DP (Recursion + Memoization) |
| 81 | + public int numSquares_3_1(int n) { |
| 82 | + int[] memo = new int[n + 1]; |
| 83 | + return helper(n, memo); |
| 84 | + } |
| 85 | + |
| 86 | + public int helper(int n, int[] memo) { |
| 87 | + if (n < 4) |
| 88 | + return n; |
| 89 | + |
| 90 | + if (memo[n] != 0) |
| 91 | + return memo[n]; |
| 92 | + |
| 93 | + int ans = n; |
| 94 | + |
| 95 | + for (int i = 1; i * i <= n; i++) { |
| 96 | + int square = i * i; |
| 97 | + ans = Math.min(ans, 1 + helper(n - square, memo)); |
| 98 | + } |
| 99 | + |
| 100 | + return memo[n] = ans; |
| 101 | + } |
| 102 | + |
| 103 | + // V3-2 |
| 104 | + // https://leetcode.com/problems/perfect-squares/solutions/2837992/java-recursion-memoization-dp-3-square-t-72qb/ |
| 105 | + // IDEA: Top Down DP (Recursion + Memoization) |
| 106 | + // Time complexity: O(N * sqrt(N)) |
| 107 | + // Space complexity: O(N) |
| 108 | + public int numSquares_3_2(int n) { |
| 109 | + int[] dp = new int[n + 1]; |
| 110 | + dp[0] = 0; |
| 111 | + |
| 112 | + for (int i = 1; i <= n; i++) { |
| 113 | + dp[i] = i; |
| 114 | + |
| 115 | + for (int j = 1; j * j <= i; j++) { |
| 116 | + int square = j * j; |
| 117 | + dp[i] = Math.min(dp[i], 1 + dp[i - square]); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return dp[n]; |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments