diff --git a/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README.md b/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README.md index f54f72d213f88..9fd1818df76b2 100644 --- a/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README.md +++ b/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README.md @@ -25,10 +25,10 @@ tags:

示例 1:

输入:n = 234
-输出:15 
+输出:15
 解释:
-各位数之积 = 2 * 3 * 4 = 24 
-各位数之和 = 2 + 3 + 4 = 9 
+各位数之积 = 2 * 3 * 4 = 24
+各位数之和 = 2 + 3 + 4 = 9
 结果 = 24 - 9 = 15
 
@@ -36,9 +36,9 @@ tags:
输入:n = 4421
 输出:21
-解释: 
-各位数之积 = 4 * 4 * 2 * 1 = 32 
-各位数之和 = 4 + 4 + 2 + 1 = 11 
+解释:
+各位数之积 = 4 * 4 * 2 * 1 = 32
+各位数之和 = 4 + 4 + 2 + 1 = 11
 结果 = 32 - 11 = 21
 
@@ -73,12 +73,8 @@ tags: ```python class Solution: def subtractProductAndSum(self, n: int) -> int: - x, y = 1, 0 - while n: - n, v = divmod(n, 10) - x *= v - y += v - return x - y + nums = list(map(int, str(n))) + return prod(nums) - sum(nums) ``` #### Java @@ -196,23 +192,4 @@ int subtractProductAndSum(int n) { - - -### 方法二 - - - -#### Python3 - -```python -class Solution: - def subtractProductAndSum(self, n: int) -> int: - nums = list(map(int, str(n))) - return prod(nums) - sum(nums) -``` - - - - - diff --git a/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README_EN.md b/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README_EN.md index b70f935f3cfce..a6251a0511bcf 100644 --- a/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README_EN.md +++ b/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README_EN.md @@ -25,10 +25,10 @@ Given an integer number n, return the difference between the produc
 Input: n = 234
-Output: 15 
-Explanation: 
-Product of digits = 2 * 3 * 4 = 24 
-Sum of digits = 2 + 3 + 4 = 9 
+Output: 15
+Explanation:
+Product of digits = 2 * 3 * 4 = 24
+Sum of digits = 2 + 3 + 4 = 9
 Result = 24 - 9 = 15
 
@@ -37,9 +37,9 @@ Result = 24 - 9 = 15
 Input: n = 4421
 Output: 21
-Explanation: 
-Product of digits = 4 * 4 * 2 * 1 = 32 
-Sum of digits = 4 + 4 + 2 + 1 = 11 
+Explanation:
+Product of digits = 4 * 4 * 2 * 1 = 32
+Sum of digits = 4 + 4 + 2 + 1 = 11
 Result = 32 - 11 = 21
 
@@ -73,12 +73,8 @@ The time complexity is $O(\log n)$, where $n$ is the given integer. The space co ```python class Solution: def subtractProductAndSum(self, n: int) -> int: - x, y = 1, 0 - while n: - n, v = divmod(n, 10) - x *= v - y += v - return x - y + nums = list(map(int, str(n))) + return prod(nums) - sum(nums) ``` #### Java @@ -196,23 +192,4 @@ int subtractProductAndSum(int n) { - - -### Solution 2 - - - -#### Python3 - -```python -class Solution: - def subtractProductAndSum(self, n: int) -> int: - nums = list(map(int, str(n))) - return prod(nums) - sum(nums) -``` - - - - - diff --git a/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/Solution2.py b/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/Solution2.py deleted file mode 100644 index c724e58f623b5..0000000000000 --- a/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/Solution2.py +++ /dev/null @@ -1,4 +0,0 @@ -class Solution: - def subtractProductAndSum(self, n: int) -> int: - nums = list(map(int, str(n))) - return prod(nums) - sum(nums) diff --git a/solution/1300-1399/1300.Sum of Mutated Array Closest to Target/README.md b/solution/1300-1399/1300.Sum of Mutated Array Closest to Target/README.md index 7df08c5743ee1..a6ae782482f5c 100644 --- a/solution/1300-1399/1300.Sum of Mutated Array Closest to Target/README.md +++ b/solution/1300-1399/1300.Sum of Mutated Array Closest to Target/README.md @@ -66,7 +66,7 @@ tags: 我们注意到,题目中要把所有大于 `value` 的值变成 `value`,并且求和,因此我们可以考虑先对数组 `arr` 进行排序,然后求出前缀和数组 $s$,其中 $s[i]$ 表示数组前 $i$ 个元素之和。 -接下来,我们可以从小到大枚举所有 `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`。 +接下来,我们可以从小到大枚举所有 `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`。 枚举完所有 `value` 后,即可得到最终答案 `ans`。 diff --git a/solution/1300-1399/1300.Sum of Mutated Array Closest to Target/README_EN.md b/solution/1300-1399/1300.Sum of Mutated Array Closest to Target/README_EN.md index a3b5eca6c9225..c6b56fbc68a9d 100644 --- a/solution/1300-1399/1300.Sum of Mutated Array Closest to Target/README_EN.md +++ b/solution/1300-1399/1300.Sum of Mutated Array Closest to Target/README_EN.md @@ -63,7 +63,15 @@ tags: -### Solution 1 +### Solution 1: Sorting + Prefix Sum + Binary Search + Enumeration + +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. + +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`. + +After enumerating all `value` values, we can get the final answer `ans`. + +Time complexity $O(n \times \log n)$, space complexity $O(n)$. Where $n$ is the length of the array `arr`. diff --git a/solution/1300-1399/1301.Number of Paths with Max Score/README_EN.md b/solution/1300-1399/1301.Number of Paths with Max Score/README_EN.md index bcba5a16b8d3b..42f8aab8492d8 100644 --- a/solution/1300-1399/1301.Number of Paths with Max Score/README_EN.md +++ b/solution/1300-1399/1301.Number of Paths with Max Score/README_EN.md @@ -66,7 +66,15 @@ tags: -### Solution 1 +### Solution 1: Dynamic Programming + +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$. + +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]$. + +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$. + +Time complexity $O(n^2)$, space complexity $O(n^2)$. Where $n$ is the side length of the array.