Skip to content

Commit 012999a

Browse files
committed
Add solution 1734
1 parent 2a16431 commit 012999a

29 files changed

+646
-432
lines changed

README.md

Lines changed: 303 additions & 303 deletions
Large diffs are not rendered by default.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leetcode
2+
3+
func decode(encoded []int) []int {
4+
n, total, odd := len(encoded), 0, 0
5+
for i := 1; i <= n+1; i++ {
6+
total ^= i
7+
}
8+
for i := 1; i < n; i += 2 {
9+
odd ^= encoded[i]
10+
}
11+
perm := make([]int, n+1)
12+
perm[0] = total ^ odd
13+
for i, v := range encoded {
14+
perm[i+1] = perm[i] ^ v
15+
}
16+
return perm
17+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1734 struct {
9+
para1734
10+
ans1734
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1734 struct {
16+
encoded []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1734 struct {
22+
one []int
23+
}
24+
25+
func Test_Problem1734(t *testing.T) {
26+
27+
qs := []question1734{
28+
29+
{
30+
para1734{[]int{3, 1}},
31+
ans1734{[]int{1, 2, 3}},
32+
},
33+
34+
{
35+
para1734{[]int{6, 5, 4, 6}},
36+
ans1734{[]int{2, 4, 1, 5, 3}},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 1734------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans1734, q.para1734
44+
fmt.Printf("【input】:%v 【output】:%v\n", p, decode(p.encoded))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# [1734. Decode XORed Permutation](https://leetcode.com/problems/decode-xored-permutation/)
2+
3+
4+
## 题目
5+
6+
There is an integer array `perm` that is a permutation of the first `n` positive integers, where `n` is always **odd**.
7+
8+
It was encoded into another integer array `encoded` of length `n - 1`, such that `encoded[i] = perm[i] XOR perm[i + 1]`. For example, if `perm = [1,3,2]`, then `encoded = [2,1]`.
9+
10+
Given the `encoded` array, return *the original array* `perm`. It is guaranteed that the answer exists and is unique.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: encoded = [3,1]
16+
Output: [1,2,3]
17+
Explanation: If perm = [1,2,3], then encoded = [1 XOR 2,2 XOR 3] = [3,1]
18+
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: encoded = [6,5,4,6]
25+
Output: [2,4,1,5,3]
26+
27+
```
28+
29+
**Constraints:**
30+
31+
- `3 <= n < 10^5`
32+
- `n` is odd.
33+
- `encoded.length == n - 1`
34+
35+
## 题目大意
36+
37+
给你一个整数数组 perm ,它是前 n 个正整数的排列,且 n 是个奇数 。它被加密成另一个长度为 n - 1 的整数数组 encoded ,满足 encoded[i] = perm[i] XOR perm[i + 1] 。比方说,如果 perm = [1,3,2] ,那么 encoded = [2,1] 。给你 encoded 数组,请你返回原始数组 perm 。题目保证答案存在且唯一。
38+
39+
## 解题思路
40+
41+
- 这一题与第 136 题和第 137 题思路类似,借用 `x ^ x = 0` 这个性质解题。依题意,原数组 perm 是 n 个正整数,即取值在 `[1,n+1]` 区间内,但是排列顺序未知。可以考虑先将 `[1,n+1]` 区间内的所有数异或得到 total。再将 encoded 数组中奇数下标的元素异或得到 odd:
42+
43+
$$\begin{aligned}odd &= encoded[1] + encoded[3] + ... + encoded[n-1]\\&= (perm[1] \,\, XOR \,\, perm[2]) + (perm[3] \,\,  XOR  \,\, perm[4]) + ... + (perm[n-1]  \,\, XOR \,\, perm[n])\end{aligned}$$
44+
45+
total 是 n 个正整数异或全集,odd 是 `n-1` 个正整数异或集。两者异或 `total ^ odd` 得到的值必定是 perm[0],因为 `x ^ x = 0`,那么重复出现的元素被异或以后消失了。算出 perm[0] 就好办了。
46+
47+
$$\begin{aligned}encoded[0] &= perm[0] \,\, XOR \,\, perm[1]\\perm[0] \,\, XOR \,\, encoded[0] &= perm[0] \,\, XOR \,\, perm[0] \,\, XOR \,\, perm[1] = perm[1]\\perm[1] \,\, XOR \,\, encoded[1] &= perm[1] \,\, XOR \,\, perm[1] \,\, XOR \,\, perm[2] = perm[2]\\...\\perm[n-1] \,\, XOR \,\, encoded[n-1] &= perm[n-1] \,\, XOR \,\, perm[n-1] \,\, XOR \,\, perm[n] = perm[n]\\\end{aligned}$$
48+
49+
依次类推,便可以推出原数组 perm 中的所有数。
50+
51+
## 代码
52+
53+
```go
54+
package leetcode
55+
56+
func decode(encoded []int) []int {
57+
n, total, odd := len(encoded), 0, 0
58+
for i := 1; i <= n+1; i++ {
59+
total ^= i
60+
}
61+
for i := 1; i < n; i += 2 {
62+
odd ^= encoded[i]
63+
}
64+
perm := make([]int, n+1)
65+
perm[0] = total ^ odd
66+
for i, v := range encoded {
67+
perm[i+1] = perm[i] ^ v
68+
}
69+
return perm
70+
}
71+
```

note/time_complexity.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,13 @@ bool isPrime (int n){
4747
}
4848
```
4949

50-
上面这段代码的时间复杂度是 O(sqrt(n)) 而不是 O(n)
50+
上面这段代码的时间复杂度是 O(sqrt(n)) 而不是 O(n)
5151

5252
再举一个例子,有一个字符串数组,将数组中的每一个字符串按照字母序排序,之后再降整个字符串数组按照字典序排序。两步操作的整体时间复杂度是多少呢?
5353

54-
如果回答是 O(n*nlog n + nlog n) = O(n^2log n),这个答案是错误的。
54+
如果回答是 O(n*nlog n + nlog n) = O(n^2log n),这个答案是错误的。字符串的长度和数组的长度是没有关系的,所以这两个变量应该单独计算。假设最长的字符串长度为 s,数组中有 n 个字符串。对每个字符串排序的时间复杂度是 O(slog s),将数组中每个字符串都按照字母序排序的时间复杂度是 O(n * slog s)。
5555

56-
字符串的长度和数组的长度是没有关系的,所以这两个变量应该单独计算。
57-
58-
假设最长的字符串长度为 s,数组中有 n 个字符串。对每个字符串排序的时间复杂度是 O(slog s),将数组中每个字符串都按照字母序排序的时间复杂度是 O(n * slog s)。
59-
60-
将整个字符串数组按照字典序排序的时间复杂度是 O(s * nlog n)。排序算法中的 O(nlog n) 是比较的次数,由于比较的是整型数字,所以每次比较是 O(1)。但是字符串按照字典序比较,时间复杂度是 O(s)。所以字符串数组按照字典序排序的时间复杂度是 O(s * nlog n)
61-
62-
所以整体复杂度是 O(n * slog s) + O(s * nlog n) = O(n\*slog s + s\*nlogn) = O(n\*s\*(log s + log n))
56+
将整个字符串数组按照字典序排序的时间复杂度是 O(s * nlog n)。排序算法中的 O(nlog n) 是比较的次数,由于比较的是整型数字,所以每次比较是 O(1)。但是字符串按照字典序比较,时间复杂度是 O(s)。所以字符串数组按照字典序排序的时间复杂度是 O(s * nlog n)。所以整体复杂度是 O(n * slog s) + O(s * nlog n) = O(n\*slog s + s\*nlogn) = O(n\*s\*(log s + log n))
6357

6458
## 二. 空间复杂度
6559

website/content/ChapterFour/0800~0899/0823.Binary-Trees-With-Factors.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5]
3939
- 首先想到的是暴力解法,先排序,然后遍历所有节点,枚举两两乘积为第三个节点值的组合。然后枚举这些组合并构成树。这里计数的时候要注意,左右孩子如果不是对称的,左右子树相互对调又是一组解。但是这个方法超时了。原因是,暴力枚举了很多次重复的节点和组合。优化这里的方法就是把已经计算过的节点放入 `map` 中。这里有 2 层 `map`,第一层 `map` 记忆化的是两两乘积的组合,将父亲节点作为 `key`,左右 2 个孩子作为 `value`。第二层 `map` 记忆化的是以 `root` 为根节点此时二叉树的种类数,`key``root``value` 存的是种类数。这样优化以后,DFS 暴力解法可以 runtime beats 100%。
4040
- 另外一种解法是 DP。定义 `dp[i]` 代表以 `i` 为根节点的树的种类数。dp[i] 初始都是 1,因为所有节点自身可以形成为自身单个节点为 `root` 的树。同样需要先排序。状态转移方程是:
4141

42-
$$dp[i] = \sum_{j<i, k<i}^{}dp[j] * dp[k], j * k = i$$
42+
{{< katex display >}}
43+
dp[i] = \sum_{j<i, k<i}^{}dp[j] * dp[k], j * k = i
44+
{{< /katex >}}
4345

4446
最后将 `dp[]` 数组中所有结果累加取模即为最终结果,时间复杂度 O(n^2),空间复杂度 O(n)。
4547

website/content/ChapterFour/1700~1799/1732.Find-the-Highest-Altitude.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ func largestAltitude(gain []int) int {
5858
----------------------------------------------
5959
<div style="display: flex;justify-content: space-between;align-items: center;">
6060
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1700~1799/1725.Number-Of-Rectangles-That-Can-Form-The-Largest-Square/">⬅️上一页</a></p>
61-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1700~1799/1736.Latest-Time-by-Replacing-Hidden-Digits/">下一页➡️</a></p>
61+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1700~1799/1734.Decode-XORed-Permutation/">下一页➡️</a></p>
6262
</div>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [1734. Decode XORed Permutation](https://leetcode.com/problems/decode-xored-permutation/)
2+
3+
4+
## 题目
5+
6+
There is an integer array `perm` that is a permutation of the first `n` positive integers, where `n` is always **odd**.
7+
8+
It was encoded into another integer array `encoded` of length `n - 1`, such that `encoded[i] = perm[i] XOR perm[i + 1]`. For example, if `perm = [1,3,2]`, then `encoded = [2,1]`.
9+
10+
Given the `encoded` array, return *the original array* `perm`. It is guaranteed that the answer exists and is unique.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: encoded = [3,1]
16+
Output: [1,2,3]
17+
Explanation: If perm = [1,2,3], then encoded = [1 XOR 2,2 XOR 3] = [3,1]
18+
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: encoded = [6,5,4,6]
25+
Output: [2,4,1,5,3]
26+
27+
```
28+
29+
**Constraints:**
30+
31+
- `3 <= n < 10^5`
32+
- `n` is odd.
33+
- `encoded.length == n - 1`
34+
35+
## 题目大意
36+
37+
给你一个整数数组 perm ,它是前 n 个正整数的排列,且 n 是个奇数 。它被加密成另一个长度为 n - 1 的整数数组 encoded ,满足 encoded[i] = perm[i] XOR perm[i + 1] 。比方说,如果 perm = [1,3,2] ,那么 encoded = [2,1] 。给你 encoded 数组,请你返回原始数组 perm 。题目保证答案存在且唯一。
38+
39+
## 解题思路
40+
41+
- 这一题与第 136 题和第 137 题思路类似,借用 `x ^ x = 0` 这个性质解题。依题意,原数组 perm 是 n 个正整数,即取值在 `[1,n+1]` 区间内,但是排列顺序未知。可以考虑先将 `[1,n+1]` 区间内的所有数异或得到 total。再将 encoded 数组中奇数下标的元素异或得到 odd:
42+
43+
{{< katex display >}}
44+
\begin{aligned}odd &= encoded[1] + encoded[3] + ... + encoded[n-1]\\&= (perm[1] \,\, XOR \,\, perm[2]) + (perm[3] \,\,  XOR  \,\, perm[4]) + ... + (perm[n-1]  \,\, XOR \,\, perm[n])\end{aligned}
45+
{{< /katex >}}
46+
47+
total 是 n 个正整数异或全集,odd 是 `n-1` 个正整数异或集。两者异或 `total ^ odd` 得到的值必定是 perm[0],因为 `x ^ x = 0`,那么重复出现的元素被异或以后消失了。算出 perm[0] 就好办了。
48+
49+
{{< katex display >}}
50+
\begin{aligned}encoded[0] &= perm[0] \,\, XOR \,\, perm[1]\\perm[0] \,\, XOR \,\, encoded[0] &= perm[0] \,\, XOR \,\, perm[0] \,\, XOR \,\, perm[1] = perm[1]\\perm[1] \,\, XOR \,\, encoded[1] &= perm[1] \,\, XOR \,\, perm[1] \,\, XOR \,\, perm[2] = perm[2]\\...\\perm[n-1] \,\, XOR \,\, encoded[n-1] &= perm[n-1] \,\, XOR \,\, perm[n-1] \,\, XOR \,\, perm[n] = perm[n]\\\end{aligned}
51+
{{< /katex >}}
52+
53+
依次类推,便可以推出原数组 perm 中的所有数。
54+
55+
## 代码
56+
57+
```go
58+
package leetcode
59+
60+
func decode(encoded []int) []int {
61+
n, total, odd := len(encoded), 0, 0
62+
for i := 1; i <= n+1; i++ {
63+
total ^= i
64+
}
65+
for i := 1; i < n; i += 2 {
66+
odd ^= encoded[i]
67+
}
68+
perm := make([]int, n+1)
69+
perm[0] = total ^ odd
70+
for i, v := range encoded {
71+
perm[i+1] = perm[i] ^ v
72+
}
73+
return perm
74+
}
75+
```
76+
77+
78+
----------------------------------------------
79+
<div style="display: flex;justify-content: space-between;align-items: center;">
80+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1700~1799/1732.Find-the-Highest-Altitude/">⬅️上一页</a></p>
81+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1700~1799/1736.Latest-Time-by-Replacing-Hidden-Digits/">下一页➡️</a></p>
82+
</div>

website/content/ChapterFour/1700~1799/1736.Latest-Time-by-Replacing-Hidden-Digits.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ func maximumTime(time string) string {
7777

7878
----------------------------------------------
7979
<div style="display: flex;justify-content: space-between;align-items: center;">
80-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1700~1799/1732.Find-the-Highest-Altitude/">⬅️上一页</a></p>
80+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1700~1799/1734.Decode-XORed-Permutation/">⬅️上一页</a></p>
8181
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1700~1799/1742.Maximum-Number-of-Balls-in-a-Box/">下一页➡️</a></p>
8282
</div>

website/content/ChapterOne/Time_Complexity.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,4 @@ int f(int n){
135135
<div style="display: flex;justify-content: space-between;align-items: center;">
136136
<p><a href="https://books.halfrost.com/leetcode/ChapterOne/Algorithm/">⬅️上一页</a></p>
137137
<p><a href="https://books.halfrost.com/leetcode/ChapterTwo/">下一章➡️</a></p>
138-
</div>
138+
</div>

0 commit comments

Comments
 (0)