Skip to content

Instantly share code, notes, and snippets.

@itarato
Created February 8, 2015 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itarato/ea1a68bdce2bac9c3c07 to your computer and use it in GitHub Desktop.
Save itarato/ea1a68bdce2bac9c3c07 to your computer and use it in GitHub Desktop.
Facebook Hacker Cup 2015 Qualification Round 1/3
package main
import (
"log"
)
func solve(digits []uint, cmp func(uint, uint) bool) []uint {
currentPos := 0
replacePos := 0
foundPos := -1
len := len(digits)
found := false
for currentPos < len-1 && !found {
replacePos = currentPos + 1
for replacePos < len {
if cmp(digits[currentPos], digits[replacePos]) && (!found || cmp(digits[foundPos], digits[replacePos])) {
foundPos = replacePos
found = true
}
replacePos += 1
}
if !found {
currentPos += 1
}
}
if found {
digits[currentPos], digits[foundPos] = digits[foundPos], digits[currentPos]
}
return digits
}
func main() {
log.Println("FB CH Cooking the books")
digits := [][]uint{
[]uint{3, 1, 2, 6, 3, 0, 4},
[]uint{9, 1, 2, 6, 3, 0, 4},
[]uint{6, 3, 2},
[]uint{1, 0},
[]uint{3, 1, 5, 2, 4},
[]uint{8, 9, 7},
[]uint{1, 2, 3},
[]uint{5},
}
for _, ex := range digits {
exmin := make([]uint, len(ex))
exmax := make([]uint, len(ex))
copy(exmin, ex)
copy(exmax, ex)
log.Println("---------------------", ex)
log.Println("Min", solve(exmin, func(a, b uint) bool { return a > b && b != 0 }))
log.Println("Max", solve(exmax, func(a, b uint) bool { return a < b }))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment