Skip to content

feat: update solutions to lc problems #3624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ tags:
<p><strong>示例 1:</strong></p>

<pre><strong>输入:</strong>n = 234
<strong>输出:</strong>15
<strong>输出:</strong>15
<strong>解释:</strong>
各位数之积 = 2 * 3 * 4 = 24
各位数之和 = 2 + 3 + 4 = 9
各位数之积 = 2 * 3 * 4 = 24
各位数之和 = 2 + 3 + 4 = 9
结果 = 24 - 9 = 15
</pre>

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

<pre><strong>输入:</strong>n = 4421
<strong>输出:</strong>21
<strong>解释:
</strong>各位数之积 = 4 * 4 * 2 * 1 = 32
各位数之和 = 4 + 4 + 2 + 1 = 11
<strong>解释:
</strong>各位数之积 = 4 * 4 * 2 * 1 = 32
各位数之和 = 4 + 4 + 2 + 1 = 11
结果 = 32 - 11 = 21
</pre>

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -196,23 +192,4 @@ int subtractProductAndSum(int n) {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def subtractProductAndSum(self, n: int) -> int:
nums = list(map(int, str(n)))
return prod(nums) - sum(nums)
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ Given an integer number <code>n</code>, return the difference between the produc

<pre>
<strong>Input:</strong> n = 234
<strong>Output:</strong> 15
<b>Explanation:</b>
Product of digits = 2 * 3 * 4 = 24
Sum of digits = 2 + 3 + 4 = 9
<strong>Output:</strong> 15
<b>Explanation:</b>
Product of digits = 2 * 3 * 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
</pre>

Expand All @@ -37,9 +37,9 @@ Result = 24 - 9 = 15
<pre>
<strong>Input:</strong> n = 4421
<strong>Output:</strong> 21
<b>Explanation:
</b>Product of digits = 4 * 4 * 2 * 1 = 32
Sum of digits = 4 + 4 + 2 + 1 = 11
<b>Explanation:
</b>Product of digits = 4 * 4 * 2 * 1 = 32
Sum of digits = 4 + 4 + 2 + 1 = 11
Result = 32 - 11 = 21
</pre>

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -196,23 +192,4 @@ int subtractProductAndSum(int n) {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Python3

```python
class Solution:
def subtractProductAndSum(self, n: int) -> int:
nums = list(map(int, str(n)))
return prod(nums) - sum(nums)
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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`。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ tags:

<!-- solution:start -->

### 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`.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ tags:

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down
Loading