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
<p>We notice that the problem requires changing all values greater than <code>value</code> to <code>value</code> and then summing them up. Therefore, we can consider sorting the array <code>arr</code> first, and then calculating the prefix sum array $s$, where $s[i]$ represents the sum of the first $i$ elements of the array.</p>
79461
+
<p>Next, we can enumerate all <code>value</code> values from smallest to largest. For each <code>value</code>, we can use binary search to find the index $i$ of the first element in the array that is greater than <code>value</code>. At this point, the number of elements in the array greater than <code>value</code> is $n - i$, so the number of elements in the array less than or equal to <code>value</code> is $i$. The sum of the elements in the array less than or equal to <code>value</code> is $s[i]$, and the sum of the elements in the array greater than <code>value</code> 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 <code>target</code> is less than the current minimum difference <code>diff</code>, update <code>diff</code> and <code>ans</code>.</p>
79462
+
<p>After enumerating all <code>value</code> values, we can get the final answer <code>ans</code>.</p>
79463
+
<p>Time complexity $O(n \times \log n)$, space complexity $O(n)$. Where $n$ is the length of the array <code>arr</code>.</p>
<p>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$.</p>
79466
+
<p>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]$.</p>
79467
+
<p>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$.</p>
79468
+
<p>Time complexity $O(n^2)$, space complexity $O(n^2)$. Where $n$ is the side length of the array.</p>
0 commit comments