-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2091-RemovingMinimumAndMaximumFromArray.go
More file actions
86 lines (75 loc) · 3.27 KB
/
2091-RemovingMinimumAndMaximumFromArray.go
File metadata and controls
86 lines (75 loc) · 3.27 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
package main
// 2091. Removing Minimum and Maximum From Array
// You are given a 0-indexed array of distinct integers nums.
// There is an element in nums that has the lowest value and an element that has the highest value.
// We call them the minimum and maximum respectively.
// Your goal is to remove both these elements from the array.
// A deletion is defined as either removing an element from the front of the array
// or removing an element from the back of the array.
// Return the minimum number of deletions it would take to remove both the minimum and maximum element from the array.
// Example 1:
// Input: nums = [2,10,7,5,4,1,8,6]
// Output: 5
// Explanation:
// The minimum element in the array is nums[5], which is 1.
// The maximum element in the array is nums[1], which is 10.
// We can remove both the minimum and maximum by removing 2 elements from the front and 3 elements from the back.
// This results in 2 + 3 = 5 deletions, which is the minimum number possible.
// Example 2:
// Input: nums = [0,-4,19,1,8,-2,-3,5]
// Output: 3
// Explanation:
// The minimum element in the array is nums[1], which is -4.
// The maximum element in the array is nums[2], which is 19.
// We can remove both the minimum and maximum by removing 3 elements from the front.
// This results in only 3 deletions, which is the minimum number possible.
// Example 3:
// Input: nums = [101]
// Output: 1
// Explanation:
// There is only one element in the array, which makes it both the minimum and maximum element.
// We can remove it with 1 deletion.
// Constraints:
// 1 <= nums.length <= 10^5
// -10^5 <= nums[i] <= 10^5
// The integers in nums are distinct.
import "fmt"
func minimumDeletions(nums []int) int {
mni, mxi, n := 0, 0, len(nums)
for i := range nums { // 找出最大最小的index
if nums[i] < nums[mni] { mni = i }
if nums[i] > nums[mxi] { mxi = i }
}
if mni > mxi {
mni, mxi = mxi, mni
}
min := func (x, y int) int { if x < y { return x; }; return y; }
return min(min(mxi + 1, n - mni), mni + n + 1 - mxi)
}
func main() {
// Example 1:
// Input: nums = [2,10,7,5,4,1,8,6]
// Output: 5
// Explanation:
// The minimum element in the array is nums[5], which is 1.
// The maximum element in the array is nums[1], which is 10.
// We can remove both the minimum and maximum by removing 2 elements from the front and 3 elements from the back.
// This results in 2 + 3 = 5 deletions, which is the minimum number possible.
fmt.Println(minimumDeletions([]int{2,10,7,5,4,1,8,6})) // 5
// Example 2:
// Input: nums = [0,-4,19,1,8,-2,-3,5]
// Output: 3
// Explanation:
// The minimum element in the array is nums[1], which is -4.
// The maximum element in the array is nums[2], which is 19.
// We can remove both the minimum and maximum by removing 3 elements from the front.
// This results in only 3 deletions, which is the minimum number possible.
fmt.Println(minimumDeletions([]int{0,-4,19,1,8,-2,-3,5})) // 3
// Example 3:
// Input: nums = [101]
// Output: 1
// Explanation:
// There is only one element in the array, which makes it both the minimum and maximum element.
// We can remove it with 1 deletion.
fmt.Println(minimumDeletions([]int{101})) // 1
}