Skip to content

Commit 7206d9c

Browse files
committed
Add solution and test-cases for problem 2161
1 parent a337ff8 commit 7206d9c

File tree

3 files changed

+52
-26
lines changed

3 files changed

+52
-26
lines changed

leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [2161.Partition Array According to Given Pivot][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** integer array `nums` and an integer `pivot`. Rearrange `nums` such that the following conditions are satisfied:
5+
6+
- Every element less than `pivot` appears **before** every element greater than `pivot`.
7+
- Every element equal to `pivot` appears **in between** the elements less than and greater than `pivot`.
8+
- The **relative order** of the elements less than `pivot` and the elements greater than `pivot` is maintained.
9+
10+
- More formally, consider every `pi`, `pj` where `pi` is the new position of the `ith` element and `pj` is the new position of the `jth` element. For elements less than `pivot`, if `i < j` and `nums[i] < pivot` and `nums[j] < pivot`, then `pi < pj`. Similarly for elements greater than `pivot`, if `i < j` and `nums[i] > pivot` and `nums[j] > pivot`, then `pi < pj`.
11+
12+
Return `nums` after the rearrangement.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: nums = [9,12,5,10,14,3,10], pivot = 10
18+
Output: [9,5,3,10,10,12,14]
19+
Explanation:
20+
The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
21+
The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
22+
The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings.
1323
```
1424

15-
## 题意
16-
> ...
25+
**Example 2:**
1726

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Partition Array According to Given Pivot
23-
```go
2427
```
25-
28+
Input: nums = [-3,4,3,2], pivot = 2
29+
Output: [-3,2,4,3]
30+
Explanation:
31+
The element -3 is less than the pivot so it is on the left side of the array.
32+
The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
33+
The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.
34+
```
2635

2736
## 结语
2837

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, pivot int) []int {
4+
i, j := 0, len(nums)-1
5+
res := make([]int, len(nums))
6+
for k := 0; k < len(nums); k++ {
7+
if nums[k] < pivot {
8+
res[i] = nums[k]
9+
i++
10+
}
11+
}
12+
for k := len(nums) - 1; k >= 0; k-- {
13+
if nums[k] > pivot {
14+
res[j] = nums[k]
15+
j--
16+
}
17+
}
18+
for ; i <= j; i++ {
19+
res[i] = pivot
20+
}
21+
return res
522
}

leetcode/2101-2200/2161.Partition-Array-According-to-Given-Pivot/Solution_test.go

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

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums, c.pivot)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.nums, c.pivot)
2828
}
2929
})
3030
}
3131
}
3232

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

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

0 commit comments

Comments
 (0)