-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2659-MakeArrayEmpty.go
More file actions
120 lines (108 loc) · 3.12 KB
/
2659-MakeArrayEmpty.go
File metadata and controls
120 lines (108 loc) · 3.12 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
package main
// 2659. Make Array Empty
// You are given an integer array nums containing distinct numbers,
// and you can perform the following operations until the array is empty:
// 1. If the first element has the smallest value, remove it
// 2. Otherwise, put the first element at the end of the array.
// Return an integer denoting the number of operations it takes to make nums empty.
// Example 1:
// Input: nums = [3,4,-1]
// Output: 5
// Operation Array
// 1 [4, -1, 3]
// 2 [-1, 3, 4]
// 3 [3, 4]
// 4 [4]
// 5 []
// Example 2:
// Input: nums = [1,2,4,3]
// Output: 5
// Operation Array
// 1 [2, 4, 3]
// 2 [4, 3]
// 3 [3, 4]
// 4 [4]
// 5 []
// Example 3:
// Input: nums = [1,2,3]
// Output: 3
// Operation Array
// 1 [2, 3]
// 2 [3]
// 3 []
// Constraints:
// 1 <= nums.length <= 10^5
// -10^9 <= nums[i] <= 10^9
// All values in nums are distinct.
import "fmt"
import "sort"
func countOperationsToEmptyArray(nums []int) int64 {
res, n := len(nums), len(nums)
index := make([][2]int, n)
for i, v := range nums {
index[i] = [2]int{ i + 1, v}
}
sort.Slice(index, func(i, j int) bool {
return index[i][1] < index[j][1]
})
for i := 1; i < n; i++ {
if index[i][0] < index[i-1][0] {
res += (n - i)
}
}
return int64(res)
}
func countOperationsToEmptyArray1(nums []int) int64 {
n := len(nums)
index := make([]int, n)
for i := range index {
index[i] = i
}
sort.Slice(index, func(i, j int) bool {
return nums[index[i]] < nums[index[j]]
})
res := n
for i := 0; i < n - 1; i++ {
if index[i + 1] < index[i] {
res += (n - i - 1)
}
}
return int64(res)
}
func main() {
// Example 1:
// Input: nums = [3,4,-1]
// Output: 5
// Operation Array
// 1 [4, -1, 3]
// 2 [-1, 3, 4]
// 3 [3, 4]
// 4 [4]
// 5 []
fmt.Println(countOperationsToEmptyArray([]int{3,4,-1})) // 5
// Example 2:
// Input: nums = [1,2,4,3]
// Output: 5
// Operation Array
// 1 [2, 4, 3]
// 2 [4, 3]
// 3 [3, 4]
// 4 [4]
// 5 []
fmt.Println(countOperationsToEmptyArray([]int{1,2,4,3})) // 5
// Example 3:
// Input: nums = [1,2,3]
// Output: 3
// Operation Array
// 1 [2, 3]
// 2 [3]
// 3 []
fmt.Println(countOperationsToEmptyArray([]int{1,2,3})) // 3
fmt.Println(countOperationsToEmptyArray([]int{1,2,3,4,5,6,7,8,9})) // 9
fmt.Println(countOperationsToEmptyArray([]int{9,8,7,6,5,4,3,2,1})) // 45
fmt.Println(countOperationsToEmptyArray1([]int{3,4,-1})) // 5
fmt.Println(countOperationsToEmptyArray1([]int{1,2,4,3})) // 5
fmt.Println(countOperationsToEmptyArray1([]int{1,2,3})) // 3
fmt.Println(countOperationsToEmptyArray1([]int{1,2,3,4,5,6,7,8,9})) // 9
fmt.Println(countOperationsToEmptyArray1([]int{9,8,7,6,5,4,3,2,1})) // 45
}