go: add «2529. Maximum Count of Positive Integer and Negative Integer»
URL: https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
2e9d570eb7
commit
3949a90cfa
1 changed files with 23 additions and 0 deletions
23
go/maximum-count-of-positive-integer-and-negative-integer.go
Normal file
23
go/maximum-count-of-positive-integer-and-negative-integer.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"slices"
|
||||
)
|
||||
|
||||
func maximumCount(nums []int) int {
|
||||
makeComparator := func(override int) func(int, int) int {
|
||||
return func(e, t int) int {
|
||||
if e == t {
|
||||
return override
|
||||
}
|
||||
|
||||
return cmp.Compare(e, t)
|
||||
}
|
||||
}
|
||||
|
||||
left, _ := slices.BinarySearchFunc(nums, 0, makeComparator(1))
|
||||
right, _ := slices.BinarySearchFunc(nums, 0, makeComparator(-1))
|
||||
|
||||
return max(left, len(nums)-right)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue