Skip to content

Commit 27ad0c8

Browse files
committed
add 279 java
1 parent 6c83038 commit 27ad0c8

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@
10491049
221| [Maximal Square](https://leetcode.com/problems/maximal-square/)|[Python](./leetcode_python/Dynamic_Programming/maximal-square.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MaximalSquare.java) | _O(n^2)_ | _O(n)_ | Medium | EPI, dp, `amazon`,`fb`, google| AGAIN** (3) (not start)
10501050
256| [Paint House](https://leetcode.com/problems/paint-house/) | [Python](./leetcode_python/Dynamic_Programming/paint-house.py) | _O(n)_| _O(1)_| Medium |🔒| AGAIN (not start)
10511051
276| [Paint Fence](https://leetcode.com/problems/paint-fence/) | [Python](./leetcode_python/Dynamic_Programming/paint-fence.py) | _O(n)_| _O(1)_| Easy |🔒| AGAIN
1052-
279| [Perfect Squares](https://leetcode.com/problems/perfect-squares/)| [Python](./leetcode_python/Dynamic_Programming/perfect-squares.py) | _O(n * sqrt(n))_ | _O(n)_ | Medium | dp, bfs, trick, Hash, `google` | AGAIN*
1052+
279| [Perfect Squares](https://leetcode.com/problems/perfect-squares/)| [Python](./leetcode_python/Dynamic_Programming/perfect-squares.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/PerfectSquares.java) | _O(n * sqrt(n))_ | _O(n)_ | Medium | dp, bfs, trick, Hash, `google` | AGAIN*
10531053
303| [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/)| [Python](./leetcode_python/Dynamic_Programming/range-sum-query-immutable.py) | ctor: _O(n)_, lookup: _O(1)_ | _O(n)_ | Easy || OK
10541054
304| [Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/)| [Python](./leetcode_python/Dynamic_Programming/range-sum-query-2d-immutable.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/RangeSumQuery2DImmutable.java) | ctor: _O(m * n)_, lookup: _O(1)_ | _O(m * n)_ | Medium |`dp`,`trick`,`fb`| AGAIN******* (5)
10551055
309| [Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) | [Python](./leetcode_python/Dynamic_Programming/best-time-to-buy-and-sell-stock-with-cooldown.py) | _O(n)_ | _O(1)_ | Medium || AGAIN (not start)

data/progress.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Progress
22

3+
# 2025-01-09
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
- TODO: go through `neetcode`, note/summary knowledges (by topic) at cheatsheet
6+
37
# 2025-01-08
48
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
59
- TODO: go through `neetcode`, note/summary knowledges (by topic) at cheatsheet

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20250109: 279,904
12
20250108: 852,692
23
20250107: 247
34
20250105: 399,1091
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

Comments
 (0)