Skip to content

Instantly share code, notes, and snippets.

@esivres
Created June 1, 2022 04:27
Show Gist options
  • Save esivres/65e3d2f0f3177f3862b0da78f8bee60c to your computer and use it in GitHub Desktop.
Save esivres/65e3d2f0f3177f3862b0da78f8bee60c to your computer and use it in GitHub Desktop.
package main
import (
"math"
"sort"
)
type el struct {
value int
first bool
}
type SortContainer struct {
elements []el
}
func (s *SortContainer) Len() int {
return len(s.elements)
}
func (s *SortContainer) Less(i, j int) bool {
return s.elements[i].value < s.elements[j].value
}
func (s *SortContainer) Swap(i, j int) {
buf := s.elements[i]
s.elements[i] = s.elements[j]
s.elements[j] = buf
}
func main() {
}
func calc(first, second []int) int {
var elements []el
for _, i := range first {
elements = append(elements, el{
value: i,
first: true,
})
}
for _, i := range second {
elements = append(elements, el{
value: i,
first: false,
})
}
container := &SortContainer{elements: elements}
sort.Sort(container)
min := math.MaxFloat64
last := container.elements[0].value
onFirst := container.elements[0].first
for _, e := range container.elements[1:] {
if onFirst != e.first {
if onFirst {
min = math.Min(math.Abs(float64(last-e.value)), min)
} else {
min = math.Min(math.Abs(float64(e.value-last)), min)
}
last = e.value
onFirst = e.first
}
}
return int(min)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment