Created
June 1, 2022 04:28
-
-
Save esivres/f8ecdb1dbc9011a46d002b79076da5f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"math" | |
"sort" | |
) | |
type el struct { | |
value int | |
isFirst 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, | |
isFirst: true, | |
}) | |
} | |
for _, i := range second { | |
elements = append(elements, el{ | |
value: i, | |
isFirst: false, | |
}) | |
} | |
container := &SortContainer{elements: elements} | |
sort.Sort(container) | |
min := math.MaxFloat64 | |
lastValue := container.elements[0].value | |
isFirst := container.elements[0].isFirst | |
for _, e := range container.elements[1:] { | |
if isFirst != e.isFirst { | |
if isFirst { | |
min = math.Min(math.Abs(float64(lastValue-e.value)), min) | |
} else { | |
min = math.Min(math.Abs(float64(e.value-lastValue)), min) | |
} | |
lastValue = e.value | |
isFirst = e.isFirst | |
}else{ | |
lastValue = e.value | |
} | |
} | |
return int(min) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment