Skip to content

Commit da1c836

Browse files
authored
Merge pull request #1024 from 0xff-dev/3011
Add solution and test-cases for problem 3011
2 parents 1cee2e7 + 38dd793 commit da1c836

File tree

3 files changed

+64
-21
lines changed

3 files changed

+64
-21
lines changed

leetcode/3001-3100/3011.Find-if-Array-Can-Be-Sorted/README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
# [3011.Find if Array Can Be Sorted][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a **0-indexed** array of **positive** integers `nums`.
5+
6+
In one **operation**, you can swap any two **adjacent** elements if they have the **same** number of `set bits`. You are allowed to do this operation **any** number of times (**including zero**).
7+
8+
Return `true` if you can sort the array, else return `false`.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [8,4,2,30,15]
14+
Output: true
15+
Explanation: Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation "10", "100", and "1000" respectively. The numbers 15 and 30 have four set bits each with binary representation "1111" and "11110".
16+
We can sort the array using 4 operations:
17+
- Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15].
18+
- Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15].
19+
- Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15].
20+
- Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30].
21+
The array has become sorted, hence we return true.
22+
Note that there may be other sequences of operations which also sort the array.
1323
```
1424

15-
## 题意
16-
> ...
17-
18-
## 题解
25+
**Example 2:**
1926

20-
### 思路1
21-
> ...
22-
Find if Array Can Be Sorted
23-
```go
2427
```
28+
Input: nums = [1,2,3,4,5]
29+
Output: true
30+
Explanation: The array is already sorted, hence we return true.
31+
```
32+
33+
**Example 3:**
2534

35+
```
36+
Input: nums = [3,16,8,4,2]
37+
Output: false
38+
Explanation: It can be shown that it is not possible to sort the input array using any number of operations.
39+
```
2640

2741
## 结语
2842

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func countOfOne3011(n int) int {
4+
c := 0
5+
for n != 0 {
6+
c++
7+
n = n & (n - 1)
8+
}
9+
return c
10+
}
11+
12+
func Solution(nums []int) bool {
13+
ma, mi := nums[0], nums[0]
14+
cur := countOfOne3011(nums[0])
15+
list := make([][2]int, 0)
16+
for i := 1; i < len(nums); i++ {
17+
now := countOfOne3011(nums[i])
18+
if now == cur {
19+
ma = max(ma, nums[i])
20+
mi = min(mi, nums[i])
21+
continue
22+
}
23+
list = append(list, [2]int{ma, mi})
24+
cur = now
25+
ma, mi = nums[i], nums[i]
26+
}
27+
list = append(list, [2]int{ma, mi})
28+
for i := 1; i < len(list); i++ {
29+
if list[i][1] <= list[i-1][0] {
30+
return false
31+
}
32+
}
33+
return true
534
}

leetcode/3001-3100/3011.Find-if-Array-Can-Be-Sorted/Solution_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs []int
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{8, 4, 2, 30, 15}, true},
17+
{"TestCase2", []int{1, 2, 3, 4, 5}, true},
18+
{"TestCase3", []int{3, 16, 8, 4, 3}, false},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)