Skip to content

Commit fa89780

Browse files
committed
feat: add solutions to lc problem: No.0279.Perfect Squares
1 parent 268fd87 commit fa89780

File tree

4 files changed

+132
-16
lines changed

4 files changed

+132
-16
lines changed

solution/0200-0299/0279.Perfect Squares/README.md

+55-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<pre>
2020
<strong>输入:</strong>n = <code>12</code>
21-
<strong>输出:</strong>3
21+
<strong>输出:</strong>3
2222
<strong>解释:</strong><code>12 = 4 + 4 + 4</code></pre>
2323

2424
<p><strong>示例 2:</strong></p>
@@ -40,22 +40,75 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43+
动态规划,定义 `dp[i]` 表示和为 `i` 的完全平方数的最少数量。
44+
4345
<!-- tabs:start -->
4446

4547
### **Python3**
4648

4749
<!-- 这里可写当前语言的特殊实现逻辑 -->
4850

4951
```python
50-
52+
class Solution:
53+
def numSquares(self, n: int) -> int:
54+
dp = [0 for i in range(n + 1)]
55+
for i in range(1, n + 1):
56+
j, mi = 1, 0x3f3f3f3f
57+
while j * j <= i:
58+
mi = min(mi, dp[i - j * j])
59+
j += 1
60+
dp[i] = mi + 1
61+
return dp[n]
5162
```
5263

5364
### **Java**
5465

5566
<!-- 这里可写当前语言的特殊实现逻辑 -->
5667

5768
```java
69+
class Solution {
70+
public int numSquares(int n) {
71+
List<Integer> ans = new ArrayList<>();
72+
ans.add(0);
73+
while (ans.size() <= n) {
74+
int m = ans.size(), val = Integer.MAX_VALUE;
75+
for (int i = 1; i * i <= m; i++) {
76+
val = Math.min(val, ans.get(m - i * i) + 1);
77+
}
78+
ans.add(val);
79+
}
80+
return ans.get(n);
81+
}
82+
}
83+
```
5884

85+
### **Go**
86+
87+
```go
88+
/*
89+
* @lc app=leetcode.cn id=279 lang=golang
90+
* 动态规划的思路,状态转移方程:dp[n] = min(dp[n-1*1]+1, dp[n-2*2]+1, ..., dp[n-k*k]+1), ( 0< k*k <=n )
91+
*/
92+
func numSquares(n int) int {
93+
if n <= 0 {
94+
return 0
95+
}
96+
dp := make([]int, n+1) // 多申请了一份整形,使代码更容易理解, dp[n] 就是 n 的完全平方数的求解
97+
for i := 1; i <= n; i++ {
98+
dp[i] = i // 初始值 dp[n] 的最大值的解,也是最容易求的解
99+
for j := 0; j*j <= i; j++ {
100+
dp[i] = minInt(dp[i-j*j]+1, dp[i])
101+
}
102+
}
103+
return dp[n]
104+
}
105+
106+
func minInt(x, y int) int {
107+
if x < y {
108+
return x
109+
}
110+
return y
111+
}
59112
```
60113

61114
### **...**

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

+54-1
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,71 @@
3535

3636
## Solutions
3737

38+
For dynamic programming, define `dp[i]` to represent the least number of perfect square numbers that sum to `i`.
39+
3840
<!-- tabs:start -->
3941

4042
### **Python3**
4143

4244
```python
43-
45+
class Solution:
46+
def numSquares(self, n: int) -> int:
47+
dp = [0 for i in range(n + 1)]
48+
for i in range(1, n + 1):
49+
j, mi = 1, 0x3f3f3f3f
50+
while j * j <= i:
51+
mi = min(mi, dp[i - j * j])
52+
j += 1
53+
dp[i] = mi + 1
54+
return dp[n]
4455
```
4556

4657
### **Java**
4758

4859
```java
60+
class Solution {
61+
public int numSquares(int n) {
62+
List<Integer> ans = new ArrayList<>();
63+
ans.add(0);
64+
while (ans.size() <= n) {
65+
int m = ans.size(), val = Integer.MAX_VALUE;
66+
for (int i = 1; i * i <= m; i++) {
67+
val = Math.min(val, ans.get(m - i * i) + 1);
68+
}
69+
ans.add(val);
70+
}
71+
return ans.get(n);
72+
}
73+
}
74+
```
4975

76+
### **Go**
77+
78+
```go
79+
/*
80+
* @lc app=leetcode.cn id=279 lang=golang
81+
* 动态规划的思路,状态转移方程:dp[n] = min(dp[n-1*1]+1, dp[n-2*2]+1, ..., dp[n-k*k]+1), ( 0< k*k <=n )
82+
*/
83+
func numSquares(n int) int {
84+
if n <= 0 {
85+
return 0
86+
}
87+
dp := make([]int, n+1) // 多申请了一份整形,使代码更容易理解, dp[n] 就是 n 的完全平方数的求解
88+
for i := 1; i <= n; i++ {
89+
dp[i] = i // 初始值 dp[n] 的最大值的解,也是最容易求的解
90+
for j := 0; j*j <= i; j++ {
91+
dp[i] = minInt(dp[i-j*j]+1, dp[i])
92+
}
93+
}
94+
return dp[n]
95+
}
96+
97+
func minInt(x, y int) int {
98+
if x < y {
99+
return x
100+
}
101+
return y
102+
}
50103
```
51104

52105
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
2-
public int numSquares(int n) {
3-
List<Integer> ans = new ArrayList<>();
4-
ans.add(0);
5-
while (ans.size() <= n) {
6-
int m = ans.size(), val = Integer.MAX_VALUE;
7-
for (int i = 1; i * i <= m; i++) {
8-
val = Math.min(val, ans.get(m - i * i) + 1);
9-
}
10-
ans.add(val);
11-
}
12-
return ans.get(n);
13-
}
14-
}
2+
public int numSquares(int n) {
3+
List<Integer> ans = new ArrayList<>();
4+
ans.add(0);
5+
while (ans.size() <= n) {
6+
int m = ans.size(), val = Integer.MAX_VALUE;
7+
for (int i = 1; i * i <= m; i++) {
8+
val = Math.min(val, ans.get(m - i * i) + 1);
9+
}
10+
ans.add(val);
11+
}
12+
return ans.get(n);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def numSquares(self, n: int) -> int:
3+
dp = [0 for i in range(n + 1)]
4+
for i in range(1, n + 1):
5+
j, mi = 1, 0x3f3f3f3f
6+
while j * j <= i:
7+
mi = min(mi, dp[i - j * j])
8+
j += 1
9+
dp[i] = mi + 1
10+
return dp[n]

0 commit comments

Comments
 (0)