406.queue-reconstruction-by-height.go 527 B

1234567891011121314151617181920212223
  1. type ints [][]int
  2. func (is ints) Len() int { return len(is) }
  3. func (is ints) Less(i, j int) bool {
  4. if is[i][0] == is[j][0] {
  5. return is[i][1] < is[j][1]
  6. }
  7. return is[i][0] > is[j][0]
  8. }
  9. func (is ints) Swap(i, j int) { is[i], is[j] = is[j], is[i] }
  10. func reconstructQueue(people [][]int) [][]int {
  11. sort.Sort(ints(people)) // Sort + insert
  12. n := len(people)
  13. for i := 0; i < n; i++ {
  14. if people[i][1] != i {
  15. tmp := people[i]
  16. copy(people[tmp[1]+1:i+1], people[tmp[1]:i])
  17. people[tmp[1]] = tmp
  18. }
  19. }
  20. return people
  21. }