Skip to content

Commit 5a549bf

Browse files
authored
feat: update solutions to lc problems (doocs#3624)
1 parent 865f078 commit 5a549bf

File tree

6 files changed

+36
-70
lines changed

6 files changed

+36
-70
lines changed

solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README.md

+8-31
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ tags:
2525
<p><strong>示例 1:</strong></p>
2626

2727
<pre><strong>输入:</strong>n = 234
28-
<strong>输出:</strong>15
28+
<strong>输出:</strong>15
2929
<strong>解释:</strong>
30-
各位数之积 = 2 * 3 * 4 = 24
31-
各位数之和 = 2 + 3 + 4 = 9
30+
各位数之积 = 2 * 3 * 4 = 24
31+
各位数之和 = 2 + 3 + 4 = 9
3232
结果 = 24 - 9 = 15
3333
</pre>
3434

3535
<p><strong>示例 2:</strong></p>
3636

3737
<pre><strong>输入:</strong>n = 4421
3838
<strong>输出:</strong>21
39-
<strong>解释:
40-
</strong>各位数之积 = 4 * 4 * 2 * 1 = 32
41-
各位数之和 = 4 + 4 + 2 + 1 = 11
39+
<strong>解释:
40+
</strong>各位数之积 = 4 * 4 * 2 * 1 = 32
41+
各位数之和 = 4 + 4 + 2 + 1 = 11
4242
结果 = 32 - 11 = 21
4343
</pre>
4444

@@ -73,12 +73,8 @@ tags:
7373
```python
7474
class Solution:
7575
def subtractProductAndSum(self, n: int) -> int:
76-
x, y = 1, 0
77-
while n:
78-
n, v = divmod(n, 10)
79-
x *= v
80-
y += v
81-
return x - y
76+
nums = list(map(int, str(n)))
77+
return prod(nums) - sum(nums)
8278
```
8379

8480
#### Java
@@ -196,23 +192,4 @@ int subtractProductAndSum(int n) {
196192
197193
<!-- solution:end -->
198194
199-
<!-- solution:start -->
200-
201-
### 方法二
202-
203-
<!-- tabs:start -->
204-
205-
#### Python3
206-
207-
```python
208-
class Solution:
209-
def subtractProductAndSum(self, n: int) -> int:
210-
nums = list(map(int, str(n)))
211-
return prod(nums) - sum(nums)
212-
```
213-
214-
<!-- tabs:end -->
215-
216-
<!-- solution:end -->
217-
218195
<!-- problem:end -->

solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README_EN.md

+9-32
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ Given an integer number <code>n</code>, return the difference between the produc
2525

2626
<pre>
2727
<strong>Input:</strong> n = 234
28-
<strong>Output:</strong> 15
29-
<b>Explanation:</b>
30-
Product of digits = 2 * 3 * 4 = 24
31-
Sum of digits = 2 + 3 + 4 = 9
28+
<strong>Output:</strong> 15
29+
<b>Explanation:</b>
30+
Product of digits = 2 * 3 * 4 = 24
31+
Sum of digits = 2 + 3 + 4 = 9
3232
Result = 24 - 9 = 15
3333
</pre>
3434

@@ -37,9 +37,9 @@ Result = 24 - 9 = 15
3737
<pre>
3838
<strong>Input:</strong> n = 4421
3939
<strong>Output:</strong> 21
40-
<b>Explanation:
41-
</b>Product of digits = 4 * 4 * 2 * 1 = 32
42-
Sum of digits = 4 + 4 + 2 + 1 = 11
40+
<b>Explanation:
41+
</b>Product of digits = 4 * 4 * 2 * 1 = 32
42+
Sum of digits = 4 + 4 + 2 + 1 = 11
4343
Result = 32 - 11 = 21
4444
</pre>
4545

@@ -73,12 +73,8 @@ The time complexity is $O(\log n)$, where $n$ is the given integer. The space co
7373
```python
7474
class Solution:
7575
def subtractProductAndSum(self, n: int) -> int:
76-
x, y = 1, 0
77-
while n:
78-
n, v = divmod(n, 10)
79-
x *= v
80-
y += v
81-
return x - y
76+
nums = list(map(int, str(n)))
77+
return prod(nums) - sum(nums)
8278
```
8379

8480
#### Java
@@ -196,23 +192,4 @@ int subtractProductAndSum(int n) {
196192
197193
<!-- solution:end -->
198194
199-
<!-- solution:start -->
200-
201-
### Solution 2
202-
203-
<!-- tabs:start -->
204-
205-
#### Python3
206-
207-
```python
208-
class Solution:
209-
def subtractProductAndSum(self, n: int) -> int:
210-
nums = list(map(int, str(n)))
211-
return prod(nums) - sum(nums)
212-
```
213-
214-
<!-- tabs:end -->
215-
216-
<!-- solution:end -->
217-
218195
<!-- problem:end -->

solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/Solution2.py

-4
This file was deleted.

solution/1300-1399/1300.Sum of Mutated Array Closest to Target/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ tags:
6666

6767
我们注意到,题目中要把所有大于 `value` 的值变成 `value`,并且求和,因此我们可以考虑先对数组 `arr` 进行排序,然后求出前缀和数组 $s$,其中 $s[i]$ 表示数组前 $i$ 个元素之和。
6868

69-
接下来,我们可以从小到大枚举所有 `value` 值,对于每个 `value`,我们可以通过二分查找找到数组中第一个大于 `value` 的元素的下标 $i$,此时数组中大于 `value` 的元素个数为 $n - i$,因此数组中小于等于 `value` 的元素个数为 $i$,此时数组中小于等于 `value` 的元素之和为 $s[i]$,数组中大于 `value` 的元素之和为 $(n - i) \times value$,因此数组中所有元素之和为 $s[i] + (n - i) \times value$。如果 $s[i] + (n - i) \times value$ 与 `target` 的差的绝对值小于当前的最小差值 `diff`,则更新 `diff``ans`
69+
接下来,我们可以从小到大枚举所有 `value` 值,对于每个 `value`,我们可以通过二分查找找到数组中第一个大于 `value` 的元素的下标 $i$,此时数组中大于 `value` 的元素个数为 $n - i$,因此数组中小于等于 `value` 的元素个数为 $i$,此时数组中小于等于 `value` 的元素之和为 $s[i]$,数组中大于 `value` 的元素之和为 $(n - i) \times \textit{value}$,因此数组中所有元素之和为 $s[i] + (n - i) \times \textit{value}$。如果 $s[i] + (n - i) \times \textit{value}$ 与 `target` 的差的绝对值小于当前的最小差值 `diff`,则更新 `diff``ans`
7070

7171
枚举完所有 `value` 后,即可得到最终答案 `ans`
7272

solution/1300-1399/1300.Sum of Mutated Array Closest to Target/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ tags:
6363

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

66-
### Solution 1
66+
### Solution 1: Sorting + Prefix Sum + Binary Search + Enumeration
67+
68+
We notice that the problem requires changing all values greater than `value` to `value` and then summing them up. Therefore, we can consider sorting the array `arr` first, and then calculating the prefix sum array $s$, where $s[i]$ represents the sum of the first $i$ elements of the array.
69+
70+
Next, we can enumerate all `value` values from smallest to largest. For each `value`, we can use binary search to find the index $i$ of the first element in the array that is greater than `value`. At this point, the number of elements in the array greater than `value` is $n - i$, so the number of elements in the array less than or equal to `value` is $i$. The sum of the elements in the array less than or equal to `value` is $s[i]$, and the sum of the elements in the array greater than `value` is $(n - i) \times value$. Therefore, the sum of all elements in the array is $s[i] + (n - i) \times \textit{value}$. If the absolute difference between $s[i] + (n - i) \times \textit{value}$ and `target` is less than the current minimum difference `diff`, update `diff` and `ans`.
71+
72+
After enumerating all `value` values, we can get the final answer `ans`.
73+
74+
Time complexity $O(n \times \log n)$, space complexity $O(n)$. Where $n$ is the length of the array `arr`.
6775

6876
<!-- tabs:start -->
6977

solution/1300-1399/1301.Number of Paths with Max Score/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ tags:
6666

6767
<!-- solution:start -->
6868

69-
### Solution 1
69+
### Solution 1: Dynamic Programming
70+
71+
We define $f[i][j]$ to represent the maximum score from the starting point $(n - 1, n - 1)$ to $(i, j)$, and $g[i][j]$ to represent the number of ways to achieve the maximum score from the starting point $(n - 1, n - 1)$ to $(i, j)$. Initially, $f[n - 1][n - 1] = 0$ and $g[n - 1][n - 1] = 1$. The other positions of $f[i][j]$ are all $-1$, and $g[i][j]$ are all $0$.
72+
73+
For the current position $(i, j)$, it can be transferred from three positions: $(i + 1, j)$, $(i, j + 1)$, and $(i + 1, j + 1)$. Therefore, we can enumerate these three positions to update the values of $f[i][j]$ and $g[i][j]$. If the current position $(i, j)$ has an obstacle, or the current position is the starting point, or other positions are out of bounds, no update is performed. Otherwise, if another position $(x, y)$ satisfies $f[x][y] \gt f[i][j]$, then we update $f[i][j] = f[x][y]$ and $g[i][j] = g[x][y]$. If $f[x][y] = f[i][j]$, then we update $g[i][j] = g[i][j] + g[x][y]$. Finally, if the current position $(i, j)$ is reachable and is a number, we update $f[i][j] = f[i][j] + board[i][j]$.
74+
75+
Finally, if $f[0][0] \lt 0$, it means there is no path to reach the endpoint, return $[0, 0]$. Otherwise, return $[f[0][0], g[0][0]]$. Note that the result needs to be taken modulo $10^9 + 7$.
76+
77+
Time complexity $O(n^2)$, space complexity $O(n^2)$. Where $n$ is the side length of the array.
7078

7179
<!-- tabs:start -->
7280

0 commit comments

Comments
 (0)