Skip to content

Commit 08a4091

Browse files
committed
feat: add solutions to lc problem: No.1049.Last Stone Weight II
1 parent b496241 commit 08a4091

File tree

5 files changed

+170
-2
lines changed

5 files changed

+170
-2
lines changed

solution/1000-1099/1049.Last Stone Weight II/README.md

+60-1
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,81 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**两个**石头的重量越接近,粉碎后的新重量就越小。同样的,**两堆**石头的重量越接近,它们粉碎后的新重量也越小。
48+
49+
所以本题可以转换为,计算容量为 `sum / 2` 的背包最多能装多少石头。
50+
4751
<!-- tabs:start -->
4852

4953
### **Python3**
5054

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

5357
```python
54-
58+
class Solution:
59+
def lastStoneWeightII(self, stones: List[int]) -> int:
60+
s = sum(stones)
61+
n = s // 2
62+
dp = [False for i in range(n + 1)]
63+
dp[0] = True
64+
for stone in stones:
65+
for j in range(n, stone - 1, -1):
66+
dp[j] = dp[j] or dp[j - stone]
67+
for j in range(n, -1, -1):
68+
if dp[j]:
69+
return s - j - j
5570
```
5671

5772
### **Java**
5873

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

6176
```java
77+
class Solution {
78+
public int lastStoneWeightII(int[] stones) {
79+
int sum = 0;
80+
for (int stone : stones) {
81+
sum += stone;
82+
}
83+
int n = sum / 2;
84+
boolean[] dp = new boolean[n + 1];
85+
dp[0] = true;
86+
for (int stone : stones) {
87+
for (int j = n; j >= stone; j--) {
88+
dp[j] = dp[j] || dp[j - stone];
89+
}
90+
}
91+
for (int j = n; ; j--) {
92+
if (dp[j]) {
93+
return sum - j - j;
94+
}
95+
}
96+
}
97+
}
98+
```
6299

100+
### **Go**
101+
102+
```go
103+
func lastStoneWeightII(stones []int) int {
104+
sum := 0
105+
for _, stone := range stones {
106+
sum += stone
107+
}
108+
n := sum / 2
109+
dp := make([]bool, n+1)
110+
dp[0] = true
111+
for _, stone := range stones {
112+
for j := n; j >= stone; j-- {
113+
dp[j] = dp[j] || dp[j-stone]
114+
}
115+
}
116+
for j := n; ; j-- {
117+
if dp[j] {
118+
return sum - j - j
119+
}
120+
}
121+
}
63122
```
64123

65124
### **...**

solution/1000-1099/1049.Last Stone Weight II/README_EN.md

+58-1
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,75 @@ we can combine 1 and 1 to get 0, so the array converts to [1], then that&#39;s t
5555

5656
## Solutions
5757

58+
This question can be converted to calculate how many stones a backpack with a capacity of `sum / 2` can hold.
59+
5860
<!-- tabs:start -->
5961

6062
### **Python3**
6163

6264
```python
63-
65+
class Solution:
66+
def lastStoneWeightII(self, stones: List[int]) -> int:
67+
s = sum(stones)
68+
n = s // 2
69+
dp = [False for i in range(n + 1)]
70+
dp[0] = True
71+
for stone in stones:
72+
for j in range(n, stone - 1, -1):
73+
dp[j] = dp[j] or dp[j - stone]
74+
for j in range(n, -1, -1):
75+
if dp[j]:
76+
return s - j - j
6477
```
6578

6679
### **Java**
6780

6881
```java
82+
class Solution {
83+
public int lastStoneWeightII(int[] stones) {
84+
int sum = 0;
85+
for (int stone : stones) {
86+
sum += stone;
87+
}
88+
int n = sum / 2;
89+
boolean[] dp = new boolean[n + 1];
90+
dp[0] = true;
91+
for (int stone : stones) {
92+
for (int j = n; j >= stone; j--) {
93+
dp[j] = dp[j] || dp[j - stone];
94+
}
95+
}
96+
for (int j = n; ; j--) {
97+
if (dp[j]) {
98+
return sum - j - j;
99+
}
100+
}
101+
}
102+
}
103+
```
69104

105+
### **Go**
106+
107+
```go
108+
func lastStoneWeightII(stones []int) int {
109+
sum := 0
110+
for _, stone := range stones {
111+
sum += stone
112+
}
113+
n := sum / 2
114+
dp := make([]bool, n+1)
115+
dp[0] = true
116+
for _, stone := range stones {
117+
for j := n; j >= stone; j-- {
118+
dp[j] = dp[j] || dp[j-stone]
119+
}
120+
}
121+
for j := n; ; j-- {
122+
if dp[j] {
123+
return sum - j - j
124+
}
125+
}
126+
}
70127
```
71128

72129
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func lastStoneWeightII(stones []int) int {
2+
sum := 0
3+
for _, stone := range stones {
4+
sum += stone
5+
}
6+
n := sum / 2
7+
dp := make([]bool, n+1)
8+
dp[0] = true
9+
for _, stone := range stones {
10+
for j := n; j >= stone; j-- {
11+
dp[j] = dp[j] || dp[j-stone]
12+
}
13+
}
14+
for j := n; ; j-- {
15+
if dp[j] {
16+
return sum - j - j
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int lastStoneWeightII(int[] stones) {
3+
int sum = 0;
4+
for (int stone : stones) {
5+
sum += stone;
6+
}
7+
int n = sum / 2;
8+
boolean[] dp = new boolean[n + 1];
9+
dp[0] = true;
10+
for (int stone : stones) {
11+
for (int j = n; j >= stone; j--) {
12+
dp[j] = dp[j] || dp[j - stone];
13+
}
14+
}
15+
for (int j = n; ; j--) {
16+
if (dp[j]) {
17+
return sum - j - j;
18+
}
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def lastStoneWeightII(self, stones: List[int]) -> int:
3+
s = sum(stones)
4+
n = s // 2
5+
dp = [False for i in range(n + 1)]
6+
dp[0] = True
7+
for stone in stones:
8+
for j in range(n, stone - 1, -1):
9+
dp[j] = dp[j] or dp[j - stone]
10+
for j in range(n, -1, -1):
11+
if dp[j]:
12+
return s - j - j

0 commit comments

Comments
 (0)