Skip to content

Commit 38b5291

Browse files
authoredJun 4, 2024
feat: update solutions to lc problems: No.412,416 (doocs#3022)
1 parent 8490191 commit 38b5291

File tree

16 files changed

+220
-181
lines changed

16 files changed

+220
-181
lines changed
 

‎solution/0400-0499/0412.Fizz Buzz/README.md

+46-26
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ tags:
6363

6464
<!-- solution:start -->
6565

66-
### 方法一
66+
### 方法一:模拟
67+
68+
我们遍历从 1 到 n 的每个整数,对于每个整数,我们检查它是否是 3 和 5 的倍数,或者只是 3 的倍数,或者只是 5 的倍数。根据检查的结果,我们将相应的字符串添加到答案数组中。
69+
70+
时间复杂度 $O(n)$,其中 $n$ 是题目给定的整数。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
6771

6872
<!-- tabs:start -->
6973

@@ -118,9 +122,15 @@ public:
118122
vector<string> ans;
119123
for (int i = 1; i <= n; ++i) {
120124
string s = "";
121-
if (i % 3 == 0) s += "Fizz";
122-
if (i % 5 == 0) s += "Buzz";
123-
if (s.size() == 0) s = to_string(i);
125+
if (i % 3 == 0) {
126+
s += "Fizz";
127+
}
128+
if (i % 5 == 0) {
129+
s += "Buzz";
130+
}
131+
if (s.empty()) {
132+
s = to_string(i);
133+
}
124134
ans.push_back(s);
125135
}
126136
return ans;
@@ -131,8 +141,7 @@ public:
131141
#### Go
132142
133143
```go
134-
func fizzBuzz(n int) []string {
135-
var ans []string
144+
func fizzBuzz(n int) (ans []string) {
136145
for i := 1; i <= n; i++ {
137146
s := &strings.Builder{}
138147
if i%3 == 0 {
@@ -146,22 +155,31 @@ func fizzBuzz(n int) []string {
146155
}
147156
ans = append(ans, s.String())
148157
}
149-
return ans
158+
return
150159
}
151160
```
152161

153162
#### JavaScript
154163

155164
```js
156-
const fizzBuzz = function (n) {
157-
let arr = [];
158-
for (let i = 1; i <= n; i++) {
159-
if (i % 15 === 0) arr.push('FizzBuzz');
160-
else if (i % 3 === 0) arr.push('Fizz');
161-
else if (i % 5 === 0) arr.push('Buzz');
162-
else arr.push(`${i}`);
165+
/**
166+
* @param {number} n
167+
* @return {string[]}
168+
*/
169+
var fizzBuzz = function (n) {
170+
const ans = [];
171+
for (let i = 1; i <= n; ++i) {
172+
if (i % 15 === 0) {
173+
ans.push('FizzBuzz');
174+
} else if (i % 3 === 0) {
175+
ans.push('Fizz');
176+
} else if (i % 5 === 0) {
177+
ans.push('Buzz');
178+
} else {
179+
ans.push(`${i}`);
180+
}
163181
}
164-
return arr;
182+
return ans;
165183
};
166184
```
167185

@@ -174,19 +192,21 @@ class Solution {
174192
* @return String[]
175193
*/
176194
function fizzBuzz($n) {
177-
$rs = [];
178-
for ($i = 1; $i <= $n; $i++) {
179-
if ($i % 3 != 0 && $i % 5 != 0) {
180-
array_push($rs, strval($i));
181-
} elseif ($i % 3 == 0 && $i % 5 != 0) {
182-
array_push($rs, 'Fizz');
183-
} elseif ($i % 3 != 0 && $i % 5 == 0) {
184-
array_push($rs, 'Buzz');
185-
} else {
186-
array_push($rs, 'FizzBuzz');
195+
$ans = [];
196+
for ($i = 1; $i <= $n; ++$i) {
197+
$s = '';
198+
if ($i % 3 == 0) {
199+
$s .= 'Fizz';
200+
}
201+
if ($i % 5 == 0) {
202+
$s .= 'Buzz';
203+
}
204+
if (strlen($s) == 0) {
205+
$s .= $i;
187206
}
207+
$ans[] = $s;
188208
}
189-
return $rs;
209+
return $ans;
190210
}
191211
}
192212
```

‎solution/0400-0499/0412.Fizz Buzz/README_EN.md

+46-26
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ tags:
5151

5252
<!-- solution:start -->
5353

54-
### Solution 1
54+
### Solution 1: Simulation
55+
56+
We iterate through each integer from 1 to $n$. For each integer, we check whether it is a multiple of both 3 and 5, or just a multiple of 3, or just a multiple of 5. Based on the check result, we add the corresponding string to the answer array.
57+
58+
The time complexity is $O(n)$, where $n$ is the integer given in the problem. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
5559

5660
<!-- tabs:start -->
5761

@@ -106,9 +110,15 @@ public:
106110
vector<string> ans;
107111
for (int i = 1; i <= n; ++i) {
108112
string s = "";
109-
if (i % 3 == 0) s += "Fizz";
110-
if (i % 5 == 0) s += "Buzz";
111-
if (s.size() == 0) s = to_string(i);
113+
if (i % 3 == 0) {
114+
s += "Fizz";
115+
}
116+
if (i % 5 == 0) {
117+
s += "Buzz";
118+
}
119+
if (s.empty()) {
120+
s = to_string(i);
121+
}
112122
ans.push_back(s);
113123
}
114124
return ans;
@@ -119,8 +129,7 @@ public:
119129
#### Go
120130
121131
```go
122-
func fizzBuzz(n int) []string {
123-
var ans []string
132+
func fizzBuzz(n int) (ans []string) {
124133
for i := 1; i <= n; i++ {
125134
s := &strings.Builder{}
126135
if i%3 == 0 {
@@ -134,22 +143,31 @@ func fizzBuzz(n int) []string {
134143
}
135144
ans = append(ans, s.String())
136145
}
137-
return ans
146+
return
138147
}
139148
```
140149

141150
#### JavaScript
142151

143152
```js
144-
const fizzBuzz = function (n) {
145-
let arr = [];
146-
for (let i = 1; i <= n; i++) {
147-
if (i % 15 === 0) arr.push('FizzBuzz');
148-
else if (i % 3 === 0) arr.push('Fizz');
149-
else if (i % 5 === 0) arr.push('Buzz');
150-
else arr.push(`${i}`);
153+
/**
154+
* @param {number} n
155+
* @return {string[]}
156+
*/
157+
var fizzBuzz = function (n) {
158+
const ans = [];
159+
for (let i = 1; i <= n; ++i) {
160+
if (i % 15 === 0) {
161+
ans.push('FizzBuzz');
162+
} else if (i % 3 === 0) {
163+
ans.push('Fizz');
164+
} else if (i % 5 === 0) {
165+
ans.push('Buzz');
166+
} else {
167+
ans.push(`${i}`);
168+
}
151169
}
152-
return arr;
170+
return ans;
153171
};
154172
```
155173

@@ -162,19 +180,21 @@ class Solution {
162180
* @return String[]
163181
*/
164182
function fizzBuzz($n) {
165-
$rs = [];
166-
for ($i = 1; $i <= $n; $i++) {
167-
if ($i % 3 != 0 && $i % 5 != 0) {
168-
array_push($rs, strval($i));
169-
} elseif ($i % 3 == 0 && $i % 5 != 0) {
170-
array_push($rs, 'Fizz');
171-
} elseif ($i % 3 != 0 && $i % 5 == 0) {
172-
array_push($rs, 'Buzz');
173-
} else {
174-
array_push($rs, 'FizzBuzz');
183+
$ans = [];
184+
for ($i = 1; $i <= $n; ++$i) {
185+
$s = '';
186+
if ($i % 3 == 0) {
187+
$s .= 'Fizz';
188+
}
189+
if ($i % 5 == 0) {
190+
$s .= 'Buzz';
191+
}
192+
if (strlen($s) == 0) {
193+
$s .= $i;
175194
}
195+
$ans[] = $s;
176196
}
177-
return $rs;
197+
return $ans;
178198
}
179199
}
180200
```

‎solution/0400-0499/0412.Fizz Buzz/Solution.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ class Solution {
44
vector<string> ans;
55
for (int i = 1; i <= n; ++i) {
66
string s = "";
7-
if (i % 3 == 0) s += "Fizz";
8-
if (i % 5 == 0) s += "Buzz";
9-
if (s.size() == 0) s = to_string(i);
7+
if (i % 3 == 0) {
8+
s += "Fizz";
9+
}
10+
if (i % 5 == 0) {
11+
s += "Buzz";
12+
}
13+
if (s.empty()) {
14+
s = to_string(i);
15+
}
1016
ans.push_back(s);
1117
}
1218
return ans;

‎solution/0400-0499/0412.Fizz Buzz/Solution.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
func fizzBuzz(n int) []string {
2-
var ans []string
1+
func fizzBuzz(n int) (ans []string) {
32
for i := 1; i <= n; i++ {
43
s := &strings.Builder{}
54
if i%3 == 0 {
@@ -13,5 +12,5 @@ func fizzBuzz(n int) []string {
1312
}
1413
ans = append(ans, s.String())
1514
}
16-
return ans
15+
return
1716
}
+17-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
const fizzBuzz = function (n) {
2-
let arr = [];
3-
for (let i = 1; i <= n; i++) {
4-
if (i % 15 === 0) arr.push('FizzBuzz');
5-
else if (i % 3 === 0) arr.push('Fizz');
6-
else if (i % 5 === 0) arr.push('Buzz');
7-
else arr.push(`${i}`);
1+
/**
2+
* @param {number} n
3+
* @return {string[]}
4+
*/
5+
var fizzBuzz = function (n) {
6+
const ans = [];
7+
for (let i = 1; i <= n; ++i) {
8+
if (i % 15 === 0) {
9+
ans.push('FizzBuzz');
10+
} else if (i % 3 === 0) {
11+
ans.push('Fizz');
12+
} else if (i % 5 === 0) {
13+
ans.push('Buzz');
14+
} else {
15+
ans.push(`${i}`);
16+
}
817
}
9-
return arr;
18+
return ans;
1019
};

‎solution/0400-0499/0412.Fizz Buzz/Solution.php

+14-12
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ class Solution {
44
* @return String[]
55
*/
66
function fizzBuzz($n) {
7-
$rs = [];
8-
for ($i = 1; $i <= $n; $i++) {
9-
if ($i % 3 != 0 && $i % 5 != 0) {
10-
array_push($rs, strval($i));
11-
} elseif ($i % 3 == 0 && $i % 5 != 0) {
12-
array_push($rs, 'Fizz');
13-
} elseif ($i % 3 != 0 && $i % 5 == 0) {
14-
array_push($rs, 'Buzz');
15-
} else {
16-
array_push($rs, 'FizzBuzz');
7+
$ans = [];
8+
for ($i = 1; $i <= $n; ++$i) {
9+
$s = '';
10+
if ($i % 3 == 0) {
11+
$s .= 'Fizz';
1712
}
13+
if ($i % 5 == 0) {
14+
$s .= 'Buzz';
15+
}
16+
if (strlen($s) == 0) {
17+
$s .= $i;
18+
}
19+
$ans[] = $s;
1820
}
19-
return $rs;
21+
return $ans;
2022
}
21-
}
23+
}

‎solution/0400-0499/0413.Arithmetic Slices/README.md

+4-31
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ tags:
6767

6868
我们用 $d$ 表示当前相邻两个元素的差值,用 $cnt$ 表示当前等差数列的长度,初始时 $d = 3000$, $cnt = 2$。
6969

70-
遍历数组 `nums`,对于相邻的两个元素 $a$ 和 $b$,如果 $b - a = d$,则说明当前元素 $b$ 也属于当前等差数列,此时 $cnt$ 自增 1;否则说明当前元素 $b$ 不属于当前等差数列,此时更新 $d = b - a$,$cnt = 2$。如果 $cnt \ge 3$,则说明当前等差数列的长度至少为 3,此时等差数列的个数为 $cnt - 2$,将其加到答案中。
70+
遍历数组 `nums`,对于相邻的两个元素 $a$ 和 $b$,如果 $b - a = d$,则说明当前元素 $b$ 也属于当前等差数列,此时 $cnt$ 自增 1;否则说明当前元素 $b$ 不属于当前等差数列,此时更新 $d = b - a$,$cnt = 2$。如果 $cnt \ge 3$,则说明当前等差数列的长度至少为 3,此时等差数列的个数为 $cnt - 2$,将其加到答案中。
7171

7272
遍历结束后,即可得到答案。
7373

@@ -87,15 +87,15 @@ tags:
8787
```python
8888
class Solution:
8989
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
90-
ans, cnt = 0, 2
90+
ans = cnt = 0
9191
d = 3000
9292
for a, b in pairwise(nums):
9393
if b - a == d:
9494
cnt += 1
9595
else:
9696
d = b - a
97-
cnt = 2
98-
ans += max(0, cnt - 2)
97+
cnt = 0
98+
ans += cnt
9999
return ans
100100
```
101101

@@ -187,31 +187,4 @@ function numberOfArithmeticSlices(nums: number[]): number {
187187

188188
<!-- solution:end -->
189189

190-
<!-- solution:start -->
191-
192-
### 方法二
193-
194-
<!-- tabs:start -->
195-
196-
#### Python3
197-
198-
```python
199-
class Solution:
200-
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
201-
ans = cnt = 0
202-
d = 3000
203-
for a, b in pairwise(nums):
204-
if b - a == d:
205-
cnt += 1
206-
else:
207-
d = b - a
208-
cnt = 0
209-
ans += cnt
210-
return ans
211-
```
212-
213-
<!-- tabs:end -->
214-
215-
<!-- solution:end -->
216-
217190
<!-- problem:end -->

‎solution/0400-0499/0413.Arithmetic Slices/README_EN.md

+19-31
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,22 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### Solution 1
60+
### Solution 1: Iteration and Counting
61+
62+
We use $d$ to represent the current difference between two adjacent elements, and $cnt$ to represent the length of the current arithmetic sequence. Initially, $d = 3000$, $cnt = 2$.
63+
64+
We iterate through the array `nums`. For two adjacent elements $a$ and $b$, if $b - a = d$, it means that the current element $b$ also belongs to the current arithmetic sequence, and we increment $cnt$ by 1. Otherwise, it means that the current element $b$ does not belong to the current arithmetic sequence, and we update $d = b - a$, and $cnt = 2$. If $cnt \ge 3$, it means that the length of the current arithmetic sequence is at least 3, and the number of arithmetic sequences is $cnt - 2$, which we add to the answer.
65+
66+
After the iteration, we can get the answer.
67+
68+
In the code implementation, we can also initialize $cnt$ to $0$, and when resetting $cnt$, we directly set $cnt$ to $0$. When adding to the answer, we directly add $cnt$.
69+
70+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of the array `nums`.
71+
72+
Similar problems:
73+
74+
- [1513. Number of Substrings With Only 1s](https://github.com/doocs/leetcode/blob/main/solution/1500-1599/1513.Number%20of%20Substrings%20With%20Only%201s/README_EN.md)
75+
- [2348. Number of Zero-Filled Subarrays](https://github.com/doocs/leetcode/blob/main/solution/2300-2399/2348.Number%20of%20Zero-Filled%20Subarrays/README_EN.md)
6176

6277
<!-- tabs:start -->
6378

@@ -66,15 +81,15 @@ tags:
6681
```python
6782
class Solution:
6883
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
69-
ans, cnt = 0, 2
84+
ans = cnt = 0
7085
d = 3000
7186
for a, b in pairwise(nums):
7287
if b - a == d:
7388
cnt += 1
7489
else:
7590
d = b - a
76-
cnt = 2
77-
ans += max(0, cnt - 2)
91+
cnt = 0
92+
ans += cnt
7893
return ans
7994
```
8095

@@ -166,31 +181,4 @@ function numberOfArithmeticSlices(nums: number[]): number {
166181

167182
<!-- solution:end -->
168183

169-
<!-- solution:start -->
170-
171-
### Solution 2
172-
173-
<!-- tabs:start -->
174-
175-
#### Python3
176-
177-
```python
178-
class Solution:
179-
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
180-
ans = cnt = 0
181-
d = 3000
182-
for a, b in pairwise(nums):
183-
if b - a == d:
184-
cnt += 1
185-
else:
186-
d = b - a
187-
cnt = 0
188-
ans += cnt
189-
return ans
190-
```
191-
192-
<!-- tabs:end -->
193-
194-
<!-- solution:end -->
195-
196184
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution:
22
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
3-
ans, cnt = 0, 2
3+
ans = cnt = 0
44
d = 3000
55
for a, b in pairwise(nums):
66
if b - a == d:
77
cnt += 1
88
else:
99
d = b - a
10-
cnt = 2
11-
ans += max(0, cnt - 2)
10+
cnt = 0
11+
ans += cnt
1212
return ans

‎solution/0400-0499/0413.Arithmetic Slices/Solution2.py

-12
This file was deleted.

‎solution/0400-0499/0414.Third Maximum Number/README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,20 @@ tags:
6363

6464
<!-- solution:start -->
6565

66-
### 方法一
66+
### 方法一:一次遍历
67+
68+
我们可以使用三个变量 $m_1$, $m_2$, $m_3$ 分别表示数组中的第一大、第二大和第三大的数。初始时,我们将这三个变量都赋值为负无穷大。
69+
70+
然后,我们遍历数组中的每个数,对于每个数,我们将其与 $m_1$, $m_2$, $m_3$ 进行比较,根据比较的结果更新这三个变量。具体地,我们遍历数组中的每个数,对于每个数:
71+
72+
- 如果这个数等于 $m_1$, $m_2$, $m_3$ 中的任何一个,我们跳过这个数;
73+
- 如果这个数大于 $m_1$,我们将 $m_1$, $m_2$, $m_3$ 的值更新为 $m_2$, $m_3$, 这个数;
74+
- 如果这个数大于 $m_2$,我们将 $m_2$, $m_3$ 的值更新为 $m_3$, 这个数;
75+
- 如果这个数大于 $m_3$,我们将 $m_3$ 的值更新为这个数。
76+
77+
最后,如果 $m_3$ 的值没有被更新,说明数组中不存在第三大的数,那么我们返回 $m_1$,否则我们返回 $m_3$。
78+
79+
时间复杂度 $O(n)$,其中 $n$ 是数组 `nums` 的长度。空间复杂度 $O(1)$。
6780

6881
<!-- tabs:start -->
6982

‎solution/0400-0499/0414.Third Maximum Number/README_EN.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,20 @@ The third distinct maximum is 1.
7070

7171
<!-- solution:start -->
7272

73-
### Solution 1
73+
### Solution 1: Single Pass
74+
75+
We can use three variables $m_1$, $m_2$, and $m_3$ to represent the first, second, and third largest numbers in the array respectively. Initially, we set these three variables to negative infinity.
76+
77+
Then, we iterate through each number in the array. For each number:
78+
79+
- If it equals any of $m_1$, $m_2$, or $m_3$, we skip this number.
80+
- If it is greater than $m_1$, we update the values of $m_1$, $m_2$, and $m_3$ to $m_2$, $m_3$, and this number respectively.
81+
- If it is greater than $m_2$, we update the values of $m_2$ and $m_3$ to $m_3$ and this number respectively.
82+
- If it is greater than $m_3$, we update the value of $m_3$ to this number.
83+
84+
Finally, if the value of $m_3$ has not been updated, it means that there is no third largest number in the array, so we return $m_1$. Otherwise, we return $m_3$.
85+
86+
The time complexity is $O(n)$, where $n$ is the length of the array `nums`. The space complexity is $O(1)$.
7487

7588
<!-- tabs:start -->
7689

‎solution/0400-0499/0416.Partition Equal Subset Sum/README.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ $$
6565

6666
最终答案为 $f[n][m]$。
6767

68-
注意到 $f[i][j]$ 只与 $f[i - 1][\cdot]$ 有关,因此我们可以将二维数组压缩成一维数组。
69-
70-
时间复杂度 $O(n \times m)$,空间复杂度 $O(m)$。其中 $n$ 是数组的长度,而 $m$ 是数组的总和的一半。
68+
时间复杂度 $(m \times n)$,空间复杂度 $(m \times n)$。其中 $m$ 和 $n$ 分别为数组的总和的一半和数组的长度。
7169

7270
<!-- tabs:start -->
7371

@@ -179,9 +177,7 @@ function canPartition(nums: number[]): boolean {
179177
}
180178
const n = nums.length;
181179
const m = s >> 1;
182-
const f: boolean[][] = Array(n + 1)
183-
.fill(0)
184-
.map(() => Array(m + 1).fill(false));
180+
const f: boolean[][] = Array.from({ length: n + 1 }, () => Array(m + 1).fill(false));
185181
f[0][0] = true;
186182
for (let i = 1; i <= n; ++i) {
187183
const x = nums[i - 1];
@@ -245,9 +241,7 @@ var canPartition = function (nums) {
245241
}
246242
const n = nums.length;
247243
const m = s >> 1;
248-
const f = Array(n + 1)
249-
.fill(0)
250-
.map(() => Array(m + 1).fill(false));
244+
const f = Array.from({ length: n + 1 }, () => Array(m + 1).fill(false));
251245
f[0][0] = true;
252246
for (let i = 1; i <= n; ++i) {
253247
const x = nums[i - 1];
@@ -265,7 +259,11 @@ var canPartition = function (nums) {
265259

266260
<!-- solution:start -->
267261

268-
### 方法二
262+
### 方法二:动态规划(空间优化)
263+
264+
我们注意到,方法一中 $f[i][j]$ 只与 $f[i - 1][\cdot]$ 有关,因此我们可以将二维数组压缩成一维数组。
265+
266+
时间复杂度 $O(n \times m)$,空间复杂度 $O(m)$。其中 $n$ 是数组的长度,而 $m$ 是数组的总和的一半。
269267

270268
<!-- tabs:start -->
271269

‎solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md

+22-8
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,21 @@ tags:
5050

5151
<!-- solution:start -->
5252

53-
### Solution 1
53+
### Solution 1: Dynamic Programming
54+
55+
First, we calculate the total sum $s$ of the array. If the total sum is odd, it cannot be divided into two subsets with equal sums, so we directly return `false`. If the total sum is even, we set the target subset sum to $m = \frac{s}{2}$. The problem is then transformed into: does there exist a subset whose element sum is $m$?
56+
57+
We define $f[i][j]$ to represent whether it is possible to select several numbers from the first $i$ numbers so that their sum is exactly $j$. Initially, $f[0][0] = true$ and the rest $f[i][j] = false$. The answer is $f[n][m]$.
58+
59+
Considering $f[i][j]$, if we select the $i$-th number $x$, then $f[i][j] = f[i - 1][j - x]$. If we do not select the $i$-th number $x$, then $f[i][j] = f[i - 1][j]$. Therefore, the state transition equation is:
60+
61+
$$
62+
f[i][j] = f[i - 1][j] \text{ or } f[i - 1][j - x] \text{ if } j \geq x
63+
$$
64+
65+
The final answer is $f[n][m]$.
66+
67+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are half of the total sum of the array and the length of the array, respectively.
5468

5569
<!-- tabs:start -->
5670

@@ -162,9 +176,7 @@ function canPartition(nums: number[]): boolean {
162176
}
163177
const n = nums.length;
164178
const m = s >> 1;
165-
const f: boolean[][] = Array(n + 1)
166-
.fill(0)
167-
.map(() => Array(m + 1).fill(false));
179+
const f: boolean[][] = Array.from({ length: n + 1 }, () => Array(m + 1).fill(false));
168180
f[0][0] = true;
169181
for (let i = 1; i <= n; ++i) {
170182
const x = nums[i - 1];
@@ -228,9 +240,7 @@ var canPartition = function (nums) {
228240
}
229241
const n = nums.length;
230242
const m = s >> 1;
231-
const f = Array(n + 1)
232-
.fill(0)
233-
.map(() => Array(m + 1).fill(false));
243+
const f = Array.from({ length: n + 1 }, () => Array(m + 1).fill(false));
234244
f[0][0] = true;
235245
for (let i = 1; i <= n; ++i) {
236246
const x = nums[i - 1];
@@ -248,7 +258,11 @@ var canPartition = function (nums) {
248258

249259
<!-- solution:start -->
250260

251-
### Solution 2
261+
### Solution 2: Dynamic Programming (Space Optimization)
262+
263+
We notice that in Solution 1, $f[i][j]$ is only related to $f[i - 1][\cdot]$. Therefore, we can compress the two-dimensional array into a one-dimensional array.
264+
265+
The time complexity is $O(n \times m)$, and the space complexity is $O(m)$. Where $n$ is the length of the array, and $m$ is half of the total sum of the array.
252266

253267
<!-- tabs:start -->
254268

‎solution/0400-0499/0416.Partition Equal Subset Sum/Solution.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ var canPartition = function (nums) {
99
}
1010
const n = nums.length;
1111
const m = s >> 1;
12-
const f = Array(n + 1)
13-
.fill(0)
14-
.map(() => Array(m + 1).fill(false));
12+
const f = Array.from({ length: n + 1 }, () => Array(m + 1).fill(false));
1513
f[0][0] = true;
1614
for (let i = 1; i <= n; ++i) {
1715
const x = nums[i - 1];

‎solution/0400-0499/0416.Partition Equal Subset Sum/Solution.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ function canPartition(nums: number[]): boolean {
55
}
66
const n = nums.length;
77
const m = s >> 1;
8-
const f: boolean[][] = Array(n + 1)
9-
.fill(0)
10-
.map(() => Array(m + 1).fill(false));
8+
const f: boolean[][] = Array.from({ length: n + 1 }, () => Array(m + 1).fill(false));
119
f[0][0] = true;
1210
for (let i = 1; i <= n; ++i) {
1311
const x = nums[i - 1];

0 commit comments

Comments
 (0)
Please sign in to comment.