Skip to content

Commit 14b3203

Browse files
committed
Used const for JavaScript code.
1 parent 05564e2 commit 14b3203

5 files changed

+74
-25
lines changed

problems/0053-maximum-subarray.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,36 @@
22
LeetCode problem: [53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)
33

44
## LeetCode problem description
5-
> Given an integer array nums, find the subarray with the largest sum, and return its sum.
5+
> Given an integer array `nums`, find the `subarray` with the largest sum, and return its sum.
66
77
```
88
Example 1:
9+
910
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
1011
Output: 6
12+
1113
Explanation: The subarray [4,-1,2,1] has the largest sum 6.
14+
------------------------------------------------------------------------
15+
16+
Example 2:
17+
18+
Input: nums = [1]
19+
Output: 1
20+
21+
Explanation: The subarray [1] has the largest sum 1.
22+
------------------------------------------------------------------------
23+
24+
Example 3:
25+
26+
Input: nums = [5,4,-1,7,8]
27+
Output: 23
28+
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
29+
------------------------------------------------------------------------
30+
31+
Constraints:
32+
33+
1. 1 <= nums.length <= 10**5
34+
2. -10**4 <= nums[i] <= 10**4
1235
```
1336

1437
## Thoughts
@@ -122,7 +145,7 @@ public class Solution {
122145
## JavaScript
123146
```javascript
124147
var maxSubArray = function(nums) {
125-
let dp = [...nums]
148+
const dp = [...nums]
126149

127150
for (let i = 1; i < dp.length; i++) {
128151
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i])

problems/0072-edit-distance.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,33 @@ You have the following three operations permitted on a word:
1111

1212
```
1313
Example 1:
14+
1415
Input: word1 = "horse", word2 = "ros"
1516
Output: 3
17+
1618
Explanation:
1719
horse -> rorse (replace 'h' with 'r')
1820
rorse -> rose (remove 'r')
1921
rose -> ros (remove 'e')
2022
-------------------------------------------------------
2123
2224
Example 2:
25+
2326
Input: word1 = "intention", word2 = "execution"
2427
Output: 5
28+
2529
Explanation:
2630
intention -> inention (remove 't')
2731
inention -> enention (replace 'i' with 'e')
2832
enention -> exention (replace 'n' with 'x')
2933
exention -> exection (replace 'n' with 'c')
3034
exection -> execution (insert 'u')
35+
-------------------------------------------------------
36+
37+
Constraints:
38+
39+
1. 0 <= word1.length, word2.length <= 500
40+
2. `word1` and `word2` consist of lowercase English letters.
3141
```
3242

3343
## Thoughts
@@ -226,7 +236,7 @@ public class Solution {
226236
## JavaScript
227237
```javascript
228238
var minDistance = function(word1, word2) {
229-
let dp = Array(word1.length + 1).fill().map(
239+
const dp = Array(word1.length + 1).fill().map(
230240
() => Array(word2.length + 1).fill(0)
231241
)
232242
dp.forEach((_, i) => { dp[i][0] = i })

problems/0392-is-subsequence.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Output: true
1414
Example 2:
1515
Input: s = "axc", t = "ahbgdc"
1616
Output: false
17+
18+
Constraints:
19+
1. 0 <= s.length <= 100
20+
2. 0 <= t.length <= 10**4
21+
3. `s` and `t` consist only of lowercase English letters.
1722
```
1823

1924
## Thoughts
@@ -174,7 +179,7 @@ public class Solution {
174179
## JavaScript
175180
```javascript
176181
var isSubsequence = function(s, t) {
177-
let dp = Array(s.length + 1).fill().map(
182+
const dp = Array(s.length + 1).fill().map(
178183
() => Array(t.length + 1).fill(false)
179184
)
180185
dp[0].fill(true)

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

+27-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
LeetCode problem: [416. Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/)
33

44
## LeetCode problem description
5-
> Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise.
5+
> Given an integer array `nums`, return `true` if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or `false` otherwise.
66
77
```
88
Example 1:
@@ -90,12 +90,12 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
9090
# 5 T T F F F T T F F F T T # dp
9191
```
9292
* After analyzing the sample `dp` grid, we can derive the `Recurrence Formula`:
93-
```python
94-
dp[j] = dp[j] or dp[j - nums[i]]
93+
```cpp
94+
dp[j] = dp[j] || dp[j - nums[i]]
9595
```
9696
4. Determine the `dp` array's traversal order
9797
* `dp[j]` depends on `dp[j]` and `dp[j - nums[i]]`, so we should traverse the `dp` array from top to bottom, then **from right to left**.
98-
* Please think if we can traverse the `dp` array from top to bottom, then `from left to right`? In the `Python` code comments, I will answer this question.
98+
* Please think if we can traverse the `dp` array from top to bottom, then `from left to right`? In the `Python` solution's code comments, I will answer this question.
9999
5. Check the `dp` array's value
100100
* Print the `dp` to see if it is as expected.
101101

@@ -108,15 +108,16 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
108108
class Solution:
109109
def canPartition(self, nums: List[int]) -> bool:
110110
sum_ = sum(nums)
111+
111112
if sum_ % 2 == 1:
112113
return False
113-
114+
114115
dp = [False] * ((sum_ // 2) + 1)
115116
dp[0] = True
116-
117+
117118
for num in nums:
118-
# If traverse from left to right, the newly assigned value `dp[j - num]` will affect the subsequent `dp[j]`.
119-
# Then it would be wrong because each `num` can only be used once.
119+
# 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.
120121
for j in range(len(dp) - 1, 0, -1):
121122
if j < num:
122123
break
@@ -131,13 +132,14 @@ class Solution {
131132
public:
132133
bool canPartition(vector<int>& nums) {
133134
auto sum = reduce(nums.begin(), nums.end());
135+
134136
if (sum % 2 == 1) {
135137
return false;
136138
}
137-
139+
138140
auto dp = vector<bool>(sum / 2 + 1);
139141
dp[0] = true;
140-
142+
141143
for (auto num : nums) {
142144
for (auto j = dp.size() - 1; j >= num; j--) {
143145
dp[j] = dp[j] || dp[j - num];
@@ -154,13 +156,14 @@ public:
154156
class Solution {
155157
public boolean canPartition(int[] nums) {
156158
var sum = IntStream.of(nums).sum();
159+
157160
if (sum % 2 == 1) {
158161
return false;
159162
}
160-
163+
161164
var dp = new boolean[sum / 2 + 1];
162165
dp[0] = true;
163-
166+
164167
for (var num : nums) {
165168
for (var j = dp.length - 1; j >= num; j--) {
166169
dp[j] = dp[j] || dp[j - num];
@@ -177,13 +180,14 @@ class Solution {
177180
public class Solution {
178181
public bool CanPartition(int[] nums) {
179182
var sum = nums.Sum();
183+
180184
if (sum % 2 == 1) {
181185
return false;
182186
}
183-
187+
184188
var dp = new bool[sum / 2 + 1];
185189
dp[0] = true;
186-
190+
187191
foreach (var num in nums) {
188192
for (var j = dp.Length - 1; j >= num; j--) {
189193
dp[j] = dp[j] || dp[j - num];
@@ -198,20 +202,21 @@ public class Solution {
198202
## JavaScript
199203
```javascript
200204
var canPartition = function(nums) {
201-
sum = _.sum(nums)
205+
const sum = _.sum(nums)
206+
202207
if (sum % 2 == 1) {
203208
return false
204209
}
205210

206-
dp = Array(sum / 2 + 1).fill(false)
211+
const dp = Array(sum / 2 + 1).fill(false)
207212
dp[0] = true
208213

209-
for (num of nums) {
214+
for (const num of nums) {
210215
for (let j = dp.length - 1; j >= num; j--) {
211216
dp[j] = dp[j] || dp[j - num]
212217
}
213218
}
214-
219+
215220
return dp.at(-1)
216221
};
217222
```
@@ -223,13 +228,14 @@ func canPartition(nums []int) bool {
223228
for _, num := range nums {
224229
sum += num
225230
}
231+
226232
if sum % 2 == 1 {
227233
return false
228234
}
229-
235+
230236
dp := make([]bool, sum / 2 + 1)
231237
dp[0] = true
232-
238+
233239
for _, num := range nums {
234240
for j := len(dp) - 1; j >= num; j-- {
235241
dp[j] = dp[j] || dp[j - num]
@@ -244,6 +250,7 @@ func canPartition(nums []int) bool {
244250
```ruby
245251
def can_partition(nums)
246252
sum = nums.sum
253+
247254
return false if sum % 2 == 1
248255

249256
dp = Array.new(sum / 2 + 1, false)

problems/0583-delete-operation-for-two-strings.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Explanation: You need one step to make "sea" to "ea" and another step to make "e
1515
Example 2:
1616
Input: word1 = "leetcode", word2 = "etco"
1717
Output: 4
18+
19+
Constraints:
20+
1. 1 <= word1.length, word2.length <= 500
21+
2. `word1` and `word2` consist of only lowercase English letters.
1822
```
1923

2024
## Thoughts
@@ -187,7 +191,7 @@ public class Solution {
187191
## JavaScript
188192
```javascript
189193
var minDistance = function(word1, word2) {
190-
let dp = Array(word1.length + 1).fill().map(
194+
const dp = Array(word1.length + 1).fill().map(
191195
() => Array(word2.length + 1).fill(0)
192196
)
193197
dp.forEach((_, i) => { dp[i][0] = i })

0 commit comments

Comments
 (0)