-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2161-PartitionArrayAccordingToGivenPivot.go
More file actions
173 lines (158 loc) · 5.98 KB
/
2161-PartitionArrayAccordingToGivenPivot.go
File metadata and controls
173 lines (158 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
// 2161. Partition Array According to Given Pivot
// You are given a 0-indexed integer array nums and an integer pivot.
// Rearrange nums such that the following conditions are satisfied:
// 1. Every element less than pivot appears before every element greater than pivot.
// 2. Every element equal to pivot appears in between the elements less than and greater than pivot.
// 3、 The relative order of the elements less than pivot and the elements greater than pivot is maintained.
// 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.
// Return nums after the rearrangement.
// Example 1:
// Input: nums = [9,12,5,10,14,3,10], pivot = 10
// Output: [9,5,3,10,10,12,14]
// Explanation:
// The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
// The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
// 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.
// Example 2:
// Input: nums = [-3,4,3,2], pivot = 2
// Output: [-3,2,4,3]
// Explanation:
// The element -3 is less than the pivot so it is on the left side of the array.
// The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
// The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.
// Constraints:
// 1 <= nums.length <= 10^5
// -10^6 <= nums[i] <= 10^6
// pivot equals to an element of nums.
import "fmt"
func pivotArray(nums []int, pivot int) []int {
n, start, middle := len(nums), 0, 0
res := make([]int, n)
for i := 0; i < n; i++ {
if nums[i] < pivot {
res[start] = nums[i]
start++
}
if nums[i] == pivot {
middle++
}
}
end := start
for i := 0; i < n; i++ {
if nums[i] > pivot {
res[start + middle] = nums[i]
middle++
}
if nums[i] == pivot {
res[end] = pivot
end++
}
}
return res
}
func pivotArray1(nums []int, pivot int) []int {
n := len(nums)
res := make([]int, n)
l, r := 0, n - 1
for i := 0; i < n; i++ {
if nums[i] < pivot {
res[l] = nums[i]
l++
} else if nums[i] > pivot {
res[r] = nums[i]
r--
}
}
reverse := func (arr []int, l, r int) {
for l < r {
arr[l], arr[r] = arr[r], arr[l]
l++
r--
}
}
reverse(res, r + 1, n - 1)
for l <= r {
res[l] = pivot
l++
}
return res
}
func pivotArray2(nums []int, pivot int) []int {
count := 0
for _, v := range nums {
if v == pivot {
count++
}
}
i, res := 0, make([]int, len(nums))
for _, v := range nums {
if v < pivot {
res[i] = v
i++
}
}
for ; count > 0; count-- {
res[i] = pivot
i++
}
for _, v := range nums {
if v > pivot {
res[i] = v
i++
}
}
return res
}
func pivotArray3(nums []int, pivot int) []int {
l, r, n := 0, len(nums) - 1, len(nums)
res := make([]int, n)
for i := 0; i < n; i++ {
res[i] = pivot
if nums[i] < pivot {
res[l] = nums[i]
l++
}
}
for i := 0; i < n; i++ {
if nums[n - 1 - i] > pivot {
res[r] = nums[n - 1 - i]
r--
}
}
return res
}
func main() {
// Example 1:
// Input: nums = [9,12,5,10,14,3,10], pivot = 10
// Output: [9,5,3,10,10,12,14]
// Explanation:
// The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
// The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
// 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.
fmt.Println(pivotArray([]int{9,12,5,10,14,3,10}, 10)) // [9,5,3,10,10,12,14]
// Example 2:
// Input: nums = [-3,4,3,2], pivot = 2
// Output: [-3,2,4,3]
// Explanation:
// The element -3 is less than the pivot so it is on the left side of the array.
// The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
// The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.
fmt.Println(pivotArray([]int{-3,4,3,2}, 2)) // [-3,2,4,3]
fmt.Println(pivotArray([]int{1,2,3,4,5,6,7,8,9}, 2)) // [1 2 3 4 5 6 7 8 9]
fmt.Println(pivotArray([]int{9,8,7,6,5,4,3,2,1}, 2)) // [1 2 9 8 7 6 5 4 3]
fmt.Println(pivotArray1([]int{9,12,5,10,14,3,10}, 10)) // [9,5,3,10,10,12,14]
fmt.Println(pivotArray1([]int{-3,4,3,2}, 2)) // [-3,2,4,3]
fmt.Println(pivotArray1([]int{1,2,3,4,5,6,7,8,9}, 2)) // [1 2 3 4 5 6 7 8 9]
fmt.Println(pivotArray1([]int{9,8,7,6,5,4,3,2,1}, 2)) // [1 2 9 8 7 6 5 4 3]
fmt.Println(pivotArray2([]int{9,12,5,10,14,3,10}, 10)) // [9,5,3,10,10,12,14]
fmt.Println(pivotArray2([]int{-3,4,3,2}, 2)) // [-3,2,4,3]
fmt.Println(pivotArray2([]int{1,2,3,4,5,6,7,8,9}, 2)) // [1 2 3 4 5 6 7 8 9]
fmt.Println(pivotArray2([]int{9,8,7,6,5,4,3,2,1}, 2)) // [1 2 9 8 7 6 5 4 3]
fmt.Println(pivotArray3([]int{9,12,5,10,14,3,10}, 10)) // [9,5,3,10,10,12,14]
fmt.Println(pivotArray3([]int{-3,4,3,2}, 2)) // [-3,2,4,3]
fmt.Println(pivotArray3([]int{1,2,3,4,5,6,7,8,9}, 2)) // [1 2 3 4 5 6 7 8 9]
fmt.Println(pivotArray3([]int{9,8,7,6,5,4,3,2,1}, 2)) // [1 2 9 8 7 6 5 4 3]
}