-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package day397 | ||
|
||
import "sort" | ||
|
||
// Job has a start and end time. | ||
type Job struct { | ||
Start, End int | ||
} | ||
|
||
// LargestSubsetCompatibleJobs returns the largest | ||
// subset of compatible jobs. Compatible jobs are non-overlapping. | ||
func LargestSubsetCompatibleJobs(jobs []Job) []Job { | ||
copied := append([]Job{}, jobs...) | ||
sort.Slice(copied, func(i, j int) bool { | ||
if copied[i].End < copied[j].End { | ||
return true | ||
} else if copied[i].End == copied[j].End { | ||
return copied[i].Start < copied[j].Start | ||
} | ||
|
||
return false | ||
}) | ||
|
||
possible := make([][]Job, 0) | ||
|
||
for _, job := range copied { | ||
added := false | ||
|
||
for i, p := range possible { | ||
left := p[len(p)-1] | ||
|
||
if job.Start >= left.End { | ||
possible[i] = append(p, job) | ||
} | ||
} | ||
|
||
if !added { | ||
possible = append(possible, []Job{job}) | ||
} | ||
} | ||
|
||
largestSize := 0 | ||
|
||
var largest []Job | ||
|
||
for _, p := range possible { | ||
if len(p) > largestSize { | ||
largestSize = len(p) | ||
largest = p | ||
} | ||
} | ||
|
||
return largest | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package day397 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
// nolint | ||
var testcases = []struct { | ||
jobs, expected []Job | ||
}{ | ||
{ | ||
[]Job{ | ||
{0, 6}, | ||
{1, 4}, | ||
{3, 5}, | ||
{3, 8}, | ||
{4, 7}, | ||
{5, 9}, | ||
{6, 10}, | ||
{8, 11}, | ||
}, | ||
[]Job{ | ||
{1, 4}, | ||
{4, 7}, | ||
{8, 11}, | ||
}, | ||
}, | ||
{ | ||
[]Job{ | ||
{0, 100}, | ||
{1, 3}, | ||
{3, 5}, | ||
{0, 10}, | ||
{6, 8}, | ||
{9, 10}, | ||
{6, 10}, | ||
{8, 11}, | ||
}, | ||
[]Job{ | ||
{1, 3}, | ||
{3, 5}, | ||
{6, 8}, | ||
{9, 10}, | ||
}, | ||
}, | ||
} | ||
|
||
func TestLargestSubsetCompatibleJobs(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range testcases { | ||
if res := LargestSubsetCompatibleJobs(tc.jobs); !reflect.DeepEqual(res, tc.expected) { | ||
t.Errorf("Expected %v, got %v", tc.expected, res) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkLargestSubsetCompatibleJobs(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
LargestSubsetCompatibleJobs(tc.jobs) | ||
} | ||
} | ||
} |