Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.
Example 1:
Input: nums = [3,2,1]
Output: 1
Explanation:
The first distinct maximum is 3.
The second distinct maximum is 2.
The third distinct maximum is 1.
Example 2:
Input: nums = [1,2]
Output: 2
Explanation:
The first distinct maximum is 2.
The second distinct maximum is 1.
The third distinct maximum does not exist, so the maximum (2) is returned instead.
Example 3:
Input: nums = [2,2,3,1]
Output: 1
Explanation:
The first distinct maximum is 3.
The second distinct maximum is 2 (both 2's are counted together since they have the same value).
The third distinct maximum is 1.
Constraints:
Follow up: Can you find an O(n) solution?
func thirdMax(nums []int) int {
first, second, third := -2147483649, -2147483649, -2147483649
for _, v := range nums {
if v > third && v != second && v != first {
third = v
}
if third > second {
second, third = third, second
}
if second > first {
first, second = second, first
}
}
if third != -2147483649 {
return third
}
return first
}
func thirdMax(nums []int) int {
min := -1<<63
a, b, c := min, min, min
for _, num := range nums {
if a < num {
c = b
b = a
a = num
}
if b < num && num < a {
c = b
b = num
}
if c < num && num < b{
c = num
}
}
if c != min {
return c
}
return a
}
func thirdMax(nums []int) int {
var first,second,third int
first = nums[0]
second = -2147483649
third = -2147483649
for _,val := range nums[1:] {
if val > first {
third = second
second = first
first = val
} else if val > second && val != first {
third = second
second = val
} else if val > third && val != second && val != first {
third = val
}
}
fmt.Println(first,second,third)
if len(nums) < 3 {
return first
}
if third == -2147483649 {
return first
}
return third
}
func thirdMax(nums []int) int {
sort.Ints(nums)
max := make([]int, 0)
i, last := len(nums)-1, 0
for i >= 0 {
if len(max) == 3 {
break
}
if nums[i] != last {
last = nums[i]
max = append(max, last)
}
i--
}
if len(max) < 3 {
return max[0]
}
return max[len(max)-1]
}
func thirdMax(nums []int) int {
max1, max2, max3 := math.MinInt, math.MinInt, math.MinInt
for _, n := range nums {
if n > max1 {
if max2 != math.MinInt { max3 = max2 }
max2 = max1
max1 = n
}
if n > max2 && n < max1 {
max3 = max2
max2 = n
}
if n > max3 && n < max2 { max3 = n }
}
if max3 != math.MinInt { return max3 }
return max1
}
func thirdMax(nums []int) int {
// DESC
max := make([]int, 0, 3)
max = append(max, nums[0])
for i:=1; i < len(nums); i++ {
max = add(max, nums[i])
}
if len(max) < 3 {
return max[0]
}
return max[2]
}
func add(max []int, m int) []int {
if len(max) == 1 {
if m == max[0] {
return max
}
if m > max[0] {
max = append(max, m)
max[0], max[1] = max[1], max[0]
} else { // m < max[0]
max = append(max, m)
}
return max
}
if len(max) == 2 {
if m == max[0] || m == max[1] {
return max
}
if m > max[0] {
max = append(max, max[1])
max[0], max[1] = m, max[0]
} else if m > max[1] {
max = append(max, max[1])
max[1] = m
} else {
max = append(max, m)
}
return max
}
// 3 elements already
if m == max[0] || m == max[1] || m == max[2] {
return max
}
if m > max[0] {
max[0], max[1], max[2] = m, max[0], max[1]
} else if m > max[1] {
max[1], max[2] = m, max[1]
} else if m > max[2] {
max[2] = m
}
return max
}
func thirdMax(nums []int) int {
var first, second, third = math.MinInt64, math.MinInt64, math.MinInt64
for _, num := range nums {
if first == num || second == num || third == num {
continue
}
if num > first {
third = second
second = first
first = num
} else if num > second {
third = second
second = num
} else if num > third {
third = num
}
}
if third == math.MinInt64 {
return first
}
return third
}
Array101 Squares of a Sorted Array in Go (0) | 2022.03.21 |
---|---|
Array 101 Find All Numbers Disappeared in an Array in Go (0) | 2022.03.19 |
Array 101 Height Checker (0) | 2022.03.17 |
Array101 Remove Element (0) | 2022.03.15 |
Array 101 Sort Array By Parity (0) | 2022.03.15 |
댓글 영역