You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0400-0499/0412.Fizz Buzz/README_EN.md
+46-26
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,11 @@ tags:
51
51
52
52
<!-- solution:start -->
53
53
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)$.
Copy file name to clipboardexpand all lines: solution/0400-0499/0413.Arithmetic Slices/README_EN.md
+19-31
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,22 @@ tags:
57
57
58
58
<!-- solution:start -->
59
59
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)
Copy file name to clipboardexpand all lines: solution/0400-0499/0414.Third Maximum Number/README_EN.md
+14-1
Original file line number
Diff line number
Diff line change
@@ -70,7 +70,20 @@ The third distinct maximum is 1.
70
70
71
71
<!-- solution:start -->
72
72
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)$.
Copy file name to clipboardexpand all lines: solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md
+22-8
Original file line number
Diff line number
Diff line change
@@ -50,7 +50,21 @@ tags:
50
50
51
51
<!-- solution:start -->
52
52
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.
54
68
55
69
<!-- tabs:start -->
56
70
@@ -162,9 +176,7 @@ function canPartition(nums: number[]): boolean {
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.
0 commit comments