Skip to content

Instantly share code, notes, and snippets.

@aquilax
Created July 22, 2018 09:11
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 aquilax/6c149a0ca407525bd78a908b34c5f850 to your computer and use it in GitHub Desktop.
Save aquilax/6c149a0ca407525bd78a908b34c5f850 to your computer and use it in GitHub Desktop.
package main
import (
"reflect"
"sort"
"testing"
)
type item struct {
from int
to int
lane int
}
func arrange(it []item) []item {
// sort first
sort.Slice(it, func(i, j int) bool { return it[i].from < it[j].from })
Loop:
for n := range it {
if n == 0 {
it[n].lane = 0
continue
}
maxLane := 0
for i := 0; i < n; i++ {
maxLane = it[i].lane
if it[n].from >= it[i].to {
it[n].lane = it[i].lane
break Loop
}
}
it[n].lane = maxLane + 1
}
return it
}
func Test_arrange(t *testing.T) {
tests := []struct {
name string
args []item
want []item
}{
{
"",
[]item{
item{1, 3, -1},
},
[]item{
item{1, 3, 0},
},
},
{
"",
[]item{
item{1, 3, -1},
item{2, 4, -1},
item{3, 4, -1},
},
[]item{
item{1, 3, 0},
item{2, 4, 1},
item{3, 4, 0},
},
},
{
"",
[]item{
item{1, 4, -1},
item{2, 4, -1},
item{3, 4, -1},
item{4, 6, -1},
},
[]item{
item{1, 4, 0},
item{2, 4, 1},
item{3, 4, 2},
item{4, 6, 0},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := arrange(tt.args); !reflect.DeepEqual(got, tt.want) {
t.Errorf("arrange() = %v, want %v", got, tt.want)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment