Created
July 22, 2018 09:11
-
-
Save aquilax/6c149a0ca407525bd78a908b34c5f850 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 ( | |
"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