-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2733-NeitherMinimumNorMaximum.go
More file actions
83 lines (71 loc) · 2.71 KB
/
2733-NeitherMinimumNorMaximum.go
File metadata and controls
83 lines (71 loc) · 2.71 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
package main
// 2733. Neither Minimum nor Maximum
// Given an integer array nums containing distinct positive integers,
// find and return any number from the array that is neither the minimum nor the maximum value in the array,
// or -1 if there is no such number.
// Return the selected integer.
// Example 1:
// Input: nums = [3,2,1,4]
// Output: 2
// Explanation: In this example, the minimum value is 1 and the maximum value is 4. Therefore, either 2 or 3 can be valid answers.
// Example 2:
// Input: nums = [1,2]
// Output: -1
// Explanation: Since there is no number in nums that is neither the maximum nor the minimum, we cannot select a number that satisfies the given condition. Therefore, there is no answer.
// Example 3:
// Input: nums = [2,1,3]
// Output: 2
// Explanation: Since 2 is neither the maximum nor the minimum value in nums, it is the only valid answer.
// Constraints:
// 1 <= nums.length <= 100
// 1 <= nums[i] <= 100
// All values in nums are distinct
import "fmt"
import "sort"
// O(nlogn)
func findNonMinOrMax(nums []int) int {
if len(nums) < 3 { return -1 }
arr := nums[:3]
sort.Ints(arr)
return arr[1]
}
// O(n)
func findNonMinOrMax1(nums []int) int {
if len(nums) <= 2 { return -1 }
res, mx, mn := 0, -1 << 31, 1 << 31
for _, v := range nums {
if v < mn { mn = v }
if v > mx {
res, mx = mx, v
} else if v > res {
res = v
}
}
return res
}
func main() {
// Example 1:
// Input: nums = [3,2,1,4]
// Output: 2
// Explanation: In this example, the minimum value is 1 and the maximum value is 4. Therefore, either 2 or 3 can be valid answers.
fmt.Println(findNonMinOrMax([]int{3,2,1,4})) // 2
// Example 2:
// Input: nums = [1,2]
// Output: -1
// Explanation: Since there is no number in nums that is neither the maximum nor the minimum, we cannot select a number that satisfies the given condition. Therefore, there is no answer.
fmt.Println(findNonMinOrMax([]int{1,2})) // -1
// Example 3:
// Input: nums = [2,1,3]
// Output: 2
// Explanation: Since 2 is neither the maximum nor the minimum value in nums, it is the only valid answer.
fmt.Println(findNonMinOrMax([]int{2,1,3})) // 2
fmt.Println(findNonMinOrMax([]int{1,1,1})) // 1
fmt.Println(findNonMinOrMax([]int{1,1,2})) // 1
fmt.Println(findNonMinOrMax([]int{1,2,2})) // 2
fmt.Println(findNonMinOrMax1([]int{3,2,1,4})) // 2
fmt.Println(findNonMinOrMax1([]int{1,2})) // -1
fmt.Println(findNonMinOrMax1([]int{2,1,3})) // 2
fmt.Println(findNonMinOrMax1([]int{1,1,1})) // 1
fmt.Println(findNonMinOrMax1([]int{1,1,2})) // 1
fmt.Println(findNonMinOrMax1([]int{1,2,2})) // 2
}