fork download
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "sort"
  6. "strconv"
  7. "strings"
  8. )
  9.  
  10. type byResultingIntValue []string
  11.  
  12. func (s byResultingIntValue) Len() int {
  13. return len(s)
  14. }
  15.  
  16. func (s byResultingIntValue) Swap(i, j int) {
  17. s[i], s[j] = s[j], s[i]
  18. }
  19.  
  20. func (s byResultingIntValue) Less(i, j int) bool {
  21. cmpInt1, _ := strconv.Atoi(s[i] + s[j])
  22. cmpInt2, _ := strconv.Atoi(s[j] + s[i])
  23. return cmpInt1 > cmpInt2
  24. }
  25.  
  26. func getMaxInteger(nums []string) string {
  27. sort.Sort(byResultingIntValue(nums))
  28. return strings.Join(nums, "")
  29. }
  30.  
  31. func getMinInteger(nums []string) string {
  32. sort.Sort(sort.Reverse(byResultingIntValue(nums)))
  33. return strings.Join(nums, "")
  34. }
  35.  
  36. func main() {
  37. nums := strings.Split("79 82 34 83 69", " ")
  38. fmt.Printf("Max: %v\n", getMaxInteger(nums))
  39. fmt.Printf("Min: %v\n", getMinInteger(nums))
  40. }
Success #stdin #stdout 0s 3100KB
stdin
Standard input is empty
stdout
Max: 8382796934
Min: 3469798283