Skip to content

Commit f6de340

Browse files
committed
1049 Removed raise exception for dynamically typed languages.
1 parent 14b3203 commit f6de340

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

problems/0416-partition-equal-subset-sum.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class Solution:
117117

118118
for num in nums:
119119
# If traversing from left to right, the newly assigned value `dp[j]` will act as `dp[j - num]` later,
120-
# then the subsequent `dp[j]` will be affected. But each `num` can only be used once.
120+
# then the subsequent `dp[j]` will be affected. But each `num` can only be used once!
121121
for j in range(len(dp) - 1, 0, -1):
122122
if j < num:
123123
break

problems/1049-last-stone-weight-ii.md

+14-19
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
7575
```
7676
* You can see the `dp` array size is one greater than the knapsack size. In this way, the backpack size and index value are equal, which helps to understand.
7777
* `dp[0]` is set to `true`, indicating that an empty backpack can be achieved by not putting any items in it. In addition, it is used as the starting value, and the subsequent `dp[j]` will depend on it. If it is `false`, all values of `dp[j]` will be `false`.
78-
* `dp[j] = false (j != 0)`, indicating that it is impossible to get `j` with no `nums`.
78+
* `dp[j] = false (j != 0)`, indicating that it is impossible to get `j` with no `stones`.
7979

8080
3. Determine the `dp` array's recurrence formula
8181
* Try to complete the grid. In the process, you will get inspiration to derive the formula.
@@ -127,7 +127,7 @@ class Solution:
127127

128128
for stone in stones:
129129
# If traversing from left to right, the newly assigned value `dp[j]` will act as `dp[j - stone]` later,
130-
# then the subsequent `dp[j]` will be affected. But each `stone` can only be used once.
130+
# then the subsequent `dp[j]` will be affected. But each `stone` can only be used once!
131131
for j in range(len(dp) - 1, 0, -1):
132132
if j < stone:
133133
break
@@ -136,8 +136,6 @@ class Solution:
136136
for i in range(len(dp) - 1, -1, -1):
137137
if dp[i]:
138138
return sum_ - i * 2
139-
140-
raise ArithmeticError("lastStoneWeightII() doesn't have a correct return value!")
141139
```
142140

143141
## C++
@@ -222,24 +220,22 @@ public class Solution {
222220
## JavaScript
223221
```javascript
224222
var lastStoneWeightII = function(stones) {
225-
const sum = _.sum(stones)
223+
const sum = _.sum(stones)
226224

227-
const dp = Array(Math.floor(sum / 2) + 1).fill(false)
228-
dp[0] = true
225+
const dp = Array(Math.floor(sum / 2) + 1).fill(false)
226+
dp[0] = true
229227

230-
for (const stone of stones) {
231-
for (let j = dp.length - 1; j >= stone; j--) {
232-
dp[j] = dp[j] || dp[j - stone]
233-
}
228+
for (const stone of stones) {
229+
for (let j = dp.length - 1; j >= stone; j--) {
230+
dp[j] = dp[j] || dp[j - stone]
234231
}
232+
}
235233

236-
for (let j = dp.length - 1; j >= 0; j--) {
237-
if (dp[j]) {
238-
return sum - j * 2
239-
}
234+
for (let j = dp.length - 1; j >= 0; j--) {
235+
if (dp[j]) {
236+
return sum - j * 2
240237
}
241-
242-
return -1; // This line should be unreachable. It represents an error state.
238+
}
243239
};
244240
```
245241

@@ -281,14 +277,13 @@ def last_stone_weight_ii(stones)
281277
stones.each do |stone|
282278
(1..(dp.size - 1)).reverse_each do |j|
283279
break if j < stone
280+
284281
dp[j] = dp[j] || dp[j - stone]
285282
end
286283
end
287284

288285
(0..(dp.size - 1)).reverse_each do |j|
289286
return sum - j * 2 if dp[j]
290287
end
291-
292-
raise "last_stone_weight_ii() doesn't have a correct return value!"
293288
end
294289
```

0 commit comments

Comments
 (0)