Skip to content

feat: add solutions to lc problem: No.3146 #3444

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
Aug 24, 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 @@ -114,9 +114,9 @@ tags:

### 方法一:二分查找 + 位运算

连续的正整数数字对应的强整数数组连接得到数组 $\textit{big\_nums}$,题目需要我们求出对于每个查询 $[\textit{left}, \textit{right}, \textit{mod}]$,子数组 $\textit{big\_nums}[\textit{left}..\textit{right}]$ 的乘积对 $\textit{mod}$ 取模的结果。由于子数组每个元素都是 $2$ 的幂,这等价于求子数组的幂次之和 $\textit{power}$,然后计算 $2^{\textit{power}} \bmod \textit{mod}$。例如,对于子数组 $[1, 4, 8]$,即 $[2^0, 2^2, 2^3]$,其幂次之和为 $0 + 2 + 3 = 5$,所以 $2^5 \bmod \textit{mod}$ 就是我们要求的结果。
连续的正整数数字对应的强整数数组连接得到数组 $\textit{bignums}$,题目需要我们求出对于每个查询 $[\textit{left}, \textit{right}, \textit{mod}]$,子数组 $\textit{bignums}[\textit{left}..\textit{right}]$ 的乘积对 $\textit{mod}$ 取模的结果。由于子数组每个元素都是 $2$ 的幂,这等价于求子数组的幂次之和 $\textit{power}$,然后计算 $2^{\textit{power}} \bmod \textit{mod}$。例如,对于子数组 $[1, 4, 8]$,即 $[2^0, 2^2, 2^3]$,其幂次之和为 $0 + 2 + 3 = 5$,所以 $2^5 \bmod \textit{mod}$ 就是我们要求的结果。

因此,我们不妨将 $\textit{big\_nums}$ 转换为幂次数组,即对于子数组 $[1, 4, 8]$,我们将其转换为 $[0, 2, 3]$。这样,问题转换为求幂次数组的子数组之和,即 $\textit{power} = \textit{f}(\textit{right} + 1) - \textit{f}(\textit{left})$,其中 $\textit{f}(i)$ 表示 $\textit{big\_nums}[0..i)$ 的幂次之和,也即是前缀和。
因此,我们不妨将 $\textit{bignums}$ 转换为幂次数组,即对于子数组 $[1, 4, 8]$,我们将其转换为 $[0, 2, 3]$。这样,问题转换为求幂次数组的子数组之和,即 $\textit{power} = \textit{f}(\textit{right} + 1) - \textit{f}(\textit{left})$,其中 $\textit{f}(i)$ 表示 $\textit{bignums}[0..i)$ 的幂次之和,也即是前缀和。

接下来,就是根据下标 $i$ 计算 $\textit{f}(i)$ 的值。我们可以使用二分查找的方法,先找到强数组长度和小于 $i$ 的最大数字,然后再计算剩下的数字的幂次之和。

Expand All @@ -140,7 +140,7 @@ tags:
| 13 | <span style="color: yellow;">1</span> | <span style="color: yellow;">1</span> | <span style="color: yellow;">0</span> | <span style="color: yellow;">1</span> |
| 14 | <span style="color: yellow;">1</span> | <span style="color: yellow;">1</span> | <span style="color: yellow;">1</span> | <span style="color: yellow;">0</span> |

将数字按照 $[2^i, 2^{i+1}-1]$ 的区间划分为不同的颜色,可以发现,区间 $[2^i, 2^{i+1}-1]$ 的数字,相当于在区间 $[0, 2^i-1]$ 的数字基础上,每个数字加上 $2^i$。我们可以根据这个规律,计算出 $\textit{big\_nums}$ 的前 $i$ 组的所有数字的强数组个数之和 $\textit{cnt}[i]$ 和幂次之和 $\textit{s}[i]$。
将数字按照 $[2^i, 2^{i+1}-1]$ 的区间划分为不同的颜色,可以发现,区间 $[2^i, 2^{i+1}-1]$ 的数字,相当于在区间 $[0, 2^i-1]$ 的数字基础上,每个数字加上 $2^i$。我们可以根据这个规律,计算出 $\textit{bignums}$ 的前 $i$ 组的所有数字的强数组个数之和 $\textit{cnt}[i]$ 和幂次之和 $\textit{s}[i]$。

接下来,对于任何数字,我们考虑如何计算其强数组的个数和幂次之和。我们可以通过二进制的方式,从最高位开始,诸位计算。例如,对于数字 $13 = 2^3 + 2^2 + 2^0$,前 $2^3$ 个数字的结果可以由 $textit{cnt}[3]$ 和 $\textit{s}[3]$ 计算得到,而剩下的 $[2^3, 13]$ 的结果,相当于给 $[0, 13-2^3]$ 的所有数字,即 $[0, 5]$ 的所有数字的强数组增加 $3$,问题转换为计算 $[0, 5]$ 的所有数字的强数组的个数和幂次之和。这样,我们可以计算出任意数字的强数组的个数和幂次之和。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ tags:

### Solution 1: Binary Search + Bit Manipulation

The continuous positive integer numbers correspond to the strong integer array, forming the array $\textit{big\_nums}$. The problem requires us to find the result of the product of the subarray $\textit{big\_nums}[\textit{left}..\textit{right}]$ modulo $\textit{mod}$ for each query $[\textit{left}, \textit{right}, \textit{mod}]$. Since each element of the subarray is a power of 2, this is equivalent to finding the sum of the powers $\textit{power}$ of the subarray, and then calculating $2^{\textit{power}} \bmod \textit{mod}$. For example, for the subarray $[1, 4, 8]$, i.e., $[2^0, 2^2, 2^3]$, the sum of the powers is $0 + 2 + 3 = 5$, so $2^5 \bmod \textit{mod}$ is the result we need.
The continuous positive integer numbers correspond to the strong integer array, forming the array $\textit{bignums}$. The problem requires us to find the result of the product of the subarray $\textit{bignums}[\textit{left}..\textit{right}]$ modulo $\textit{mod}$ for each query $[\textit{left}, \textit{right}, \textit{mod}]$. Since each element of the subarray is a power of 2, this is equivalent to finding the sum of the powers $\textit{power}$ of the subarray, and then calculating $2^{\textit{power}} \bmod \textit{mod}$. For example, for the subarray $[1, 4, 8]$, i.e., $[2^0, 2^2, 2^3]$, the sum of the powers is $0 + 2 + 3 = 5$, so $2^5 \bmod \textit{mod}$ is the result we need.

Therefore, we can convert $\textit{big\_nums}$ into an array of powers. For example, for the subarray $[1, 4, 8]$, we convert it to $[0, 2, 3]$. Thus, the problem is transformed into finding the sum of the subarray of powers, i.e., $\textit{power} = \textit{f}(\textit{right} + 1) - \textit{f}(\textit{left})$, where $\textit{f}(i)$ represents the sum of the powers of $\textit{big\_nums}[0..i)$, which is the prefix sum.
Therefore, we can convert $\textit{bignums}$ into an array of powers. For example, for the subarray $[1, 4, 8]$, we convert it to $[0, 2, 3]$. Thus, the problem is transformed into finding the sum of the subarray of powers, i.e., $\textit{power} = \textit{f}(\textit{right} + 1) - \textit{f}(\textit{left})$, where $\textit{f}(i)$ represents the sum of the powers of $\textit{bignums}[0..i)$, which is the prefix sum.

Next, we calculate the value of $\textit{f}(i)$ based on the index $i$. We can use binary search to find the largest number whose strong array length is less than $i$, and then calculate the sum of the powers of the remaining numbers.

Expand All @@ -138,7 +138,7 @@ According to the problem description, we list the strong integers for numbers $0
| 13 | <span style="color: yellow;">1</span> | <span style="color: yellow;">1</span> | <span style="color: yellow;">0</span> | <span style="color: yellow;">1</span> |
| 14 | <span style="color: yellow;">1</span> | <span style="color: yellow;">1</span> | <span style="color: yellow;">1</span> | <span style="color: yellow;">0</span> |

By dividing the numbers into different colors according to the interval $[2^i, 2^{i+1}-1]$, we can see that the numbers in the interval $[2^i, 2^{i+1}-1]$ are equivalent to adding $2^i$ to each number in the interval $[0, 2^i-1]$. Based on this pattern, we can calculate the total number of strong arrays $\textit{cnt}[i]$ and the sum of powers $\textit{s}[i]$ for the first $i$ groups of numbers in $\textit{big\_nums}$.
By dividing the numbers into different colors according to the interval $[2^i, 2^{i+1}-1]$, we can see that the numbers in the interval $[2^i, 2^{i+1}-1]$ are equivalent to adding $2^i$ to each number in the interval $[0, 2^i-1]$. Based on this pattern, we can calculate the total number of strong arrays $\textit{cnt}[i]$ and the sum of powers $\textit{s}[i]$ for the first $i$ groups of numbers in $\textit{bignums}$.

Next, for any number, we consider how to calculate the number of strong arrays and the sum of powers. We can use the binary method, calculating from the highest bit. For example, for the number $13 = 2^3 + 2^2 + 2^0$, the result of the first $2^3$ numbers can be obtained from $\textit{cnt}[3]$ and $\textit{s}[3]$, and the result of the remaining $[2^3, 13]$ is equivalent to adding $3$ to all numbers in $[0, 13-2^3]$, i.e., $[0, 5]$. The problem is transformed into calculating the number of strong arrays and the sum of powers for $[0, 5]$. In this way, we can calculate the number of strong arrays and the sum of powers for any number.

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

<!-- solution:start -->

### 方法一
### 方法一:哈希表或数组

我们可以使用哈希表或者一个长度为 $26$ 的数组 $\textit{d}$ 来存储字符串 $\textit{s}$ 中每个字符的位置。

然后遍历字符串 $\textit{t}$,计算每个字符在字符串 $\textit{t}$ 中的位置与在字符串 $\textit{s}$ 中的位置之差的绝对值之和即可。

时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{s}$ 的长度。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 为字符集,这里是小写英文字母,所以 $|\Sigma| \leq 26$。

<!-- tabs:start -->

Expand Down Expand Up @@ -148,16 +154,35 @@ function findPermutationDifference(s: string, t: string): number {
const d: number[] = Array(26).fill(0);
const n = s.length;
for (let i = 0; i < n; ++i) {
d[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i;
d[s.charCodeAt(i) - 97] = i;
}
let ans = 0;
for (let i = 0; i < n; ++i) {
ans += Math.abs(d[t.charCodeAt(i) - 'a'.charCodeAt(0)] - i);
ans += Math.abs(d[t.charCodeAt(i) - 97] - i);
}
return ans;
}
```

#### C#

```cs
public class Solution {
public int FindPermutationDifference(string s, string t) {
int[] d = new int[26];
int n = s.Length;
for (int i = 0; i < n; ++i) {
d[s[i] - 'a'] = i;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += Math.Abs(d[t[i] - 'a'] - i);
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table or Array

We can use a hash table or an array of length $26$, denoted as $\textit{d}$, to store the positions of each character in the string $\textit{s}$.

Then, we traverse the string $\textit{t}$ and calculate the sum of the absolute differences between the positions of each character in the string $\textit{t}$ and the positions in the string $\textit{s}$.

The time complexity is $O(n)$, where $n$ is the length of the string $\textit{s}$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set. Here, it is lowercase English letters, so $|\Sigma| \leq 26$.

<!-- tabs:start -->

Expand Down Expand Up @@ -146,16 +152,35 @@ function findPermutationDifference(s: string, t: string): number {
const d: number[] = Array(26).fill(0);
const n = s.length;
for (let i = 0; i < n; ++i) {
d[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i;
d[s.charCodeAt(i) - 97] = i;
}
let ans = 0;
for (let i = 0; i < n; ++i) {
ans += Math.abs(d[t.charCodeAt(i) - 'a'.charCodeAt(0)] - i);
ans += Math.abs(d[t.charCodeAt(i) - 97] - i);
}
return ans;
}
```

#### C#

```cs
public class Solution {
public int FindPermutationDifference(string s, string t) {
int[] d = new int[26];
int n = s.Length;
for (int i = 0; i < n; ++i) {
d[s[i] - 'a'] = i;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += Math.Abs(d[t[i] - 'a'] - i);
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Solution {
public int FindPermutationDifference(string s, string t) {
int[] d = new int[26];
int n = s.Length;
for (int i = 0; i < n; ++i) {
d[s[i] - 'a'] = i;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += Math.Abs(d[t[i] - 'a'] - i);
}
return ans;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ function findPermutationDifference(s: string, t: string): number {
const d: number[] = Array(26).fill(0);
const n = s.length;
for (let i = 0; i < n; ++i) {
d[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i;
d[s.charCodeAt(i) - 97] = i;
}
let ans = 0;
for (let i = 0; i < n; ++i) {
ans += Math.abs(d[t.charCodeAt(i) - 'a'.charCodeAt(0)] - i);
ans += Math.abs(d[t.charCodeAt(i) - 97] - i);
}
return ans;
}
Loading