Skip to content
Open
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 @@ -69,13 +69,13 @@ tags:

<!-- solution:start -->

### 方法一:数学 + 位运算
### Solution 1: Math + Bit Manipulation

题目中 $\textit{left}$ $\textit{right}$ 的范围均在 $10^6$ 以内,而 $2^{20} = 1048576$,因此,二进制中 $1$ 的个数最多也就 $20$ 个,而 $20$ 以内的质数有 $[2, 3, 5, 7, 11, 13, 17, 19]$
In the problem, both $\textit{left}$ and $\textit{right}$ are within the range of $10^6$, and since $2^{20} = 1048576$, the number of $1$s in binary representation can be at most $20$. The prime numbers within $20$ are $[2, 3, 5, 7, 11, 13, 17, 19]$.

我们枚举 $[\textit{left},.. \textit{right}]$ 范围内的每个数,统计其二进制中 $1$ 的个数,然后判断该个数是否为质数,如果是,答案加一。
We enumerate each number in the range $[\textit{left},.. \textit{right}]$, count the number of $1$s in its binary representation, and then check if this count is a prime number. If it is, we increment the answer by one.

时间复杂度 $O(n\times \log m)$。其中 $n = \textit{right} - \textit{left} + 1$,而 $m$ $[\textit{left},.. \textit{right}]$ 范围内的最大数。
The time complexity is $O(n\times \log m)$, where $n = \textit{right} - \textit{left} + 1$ and $m$ is the maximum number in the range $[\textit{left},.. \textit{right}]$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ The total wasted space is (20 - 10) + (20 - 20) = 10.
<strong>Input:</strong> nums = [10,20,30], k = 1
<strong>Output:</strong> 10
<strong>Explanation:</strong> size = [20,20,30].
We can set the initial size to be 20 and resize to 30 at time 2.
We can set the initial size to be 20 and resize to 30 at time 2.
The total wasted space is (20 - 10) + (20 - 20) + (30 - 30) = 10.
</pre>

Expand Down Expand Up @@ -73,7 +73,8 @@ The total wasted space is (10 - 10) + (20 - 20) + (20 - 15) + (30 - 30) + (30 -

<!-- solution:start -->

Solution 1: Dynamic Programming
### Solution 1: Dynamic Programming

The problem is equivalent to dividing the array $\textit{nums}$ into $k + 1$ segments. The wasted space for each segment is the maximum value of that segment multiplied by the length of the segment minus the sum of the elements in that segment. By summing the wasted space of each segment, we get the total wasted space. By adding 1 to $k$, we are effectively dividing the array into $k$ segments.

Therefore, we define an array $\textit{g}[i][j]$ to represent the wasted space for the segment $\textit{nums}[i..j]$, which is the maximum value of $\textit{nums}[i..j]$ multiplied by the length of $\textit{nums}[i..j]$ minus the sum of the elements in $\textit{nums}[i..j]$. We iterate over $i$ in the range $[0, n)$ and $j$ in the range $[i, n)$, using a variable $s$ to maintain the sum of the elements in $\textit{nums}[i..j]$ and a variable $\textit{mx}$ to maintain the maximum value of $\textit{nums}[i..j]$. Then we can get:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [1,15,6,3]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
<strong>Explanation:</strong>
The element sum of nums is 1 + 15 + 6 + 3 = 25.
The digit sum of nums is 1 + 1 + 5 + 6 + 3 = 16.
The absolute difference between the element sum and digit sum is |25 - 16| = 9.
Expand Down Expand Up @@ -67,7 +67,8 @@ The absolute difference between the element sum and digit sum is |10 - 10| = 0.

<!-- solution:start -->

Solution 1: Simulation
### Solution 1: Simulation

We traverse the array $\textit{nums}$, calculate the sum of the elements $x$ and the sum of the digits $y$, and finally return $|x - y|$. Since $x$ is always greater than or equal to $y$, we can directly return $x - y$.

The time complexity is $O(n \times \log_{10} M)$, where $n$ and $M$ are the length of the array $\textit{nums}$ and the maximum value of the elements in the array, respectively. The space complexity is $O(1)$.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ tags:

<!-- solution:start -->

Solution 1: Fast Matrix Exponentiation to Accelerate Recurrence
### Solution 1: Fast Matrix Exponentiation to Accelerate Recurrence

We define $f[i][j]$ as the number of times the $j$-th letter appears in the alphabet after $i$ transformations. Initially, $f[0][j]$ corresponds to the frequency of the $j$-th letter in the input string $s$.

Since the frequency of each letter after a transformation affects the next transformation, and the total number of transformations $t$ can be large, we can accelerate this recurrence process using fast matrix exponentiation.
Expand Down