forked from samarthgaur2/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivity_Selection.go
35 lines (31 loc) · 958 Bytes
/
Activity_Selection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"fmt"
"strconv"
)
// this algorithm is from wikipage(https://en.wikipedia.org/wiki/Activity_selection_problem)
func activitySelection(startTime []int, finishTime []int) {
// second step is to print first activity from sorted activity
fmt.Println("activity 1")
// do following in sorted activities:
// If the start time of this activity is greater than the finish time of previously
// selected activity then select this activity and print it
preSelected := 0
for i := 1; i < len(finishTime); i++ {
if startTime[i] > finishTime[preSelected] {
fmt.Println("activity", strconv.Itoa(i + 1))
preSelected = i
}
}
}
func main() {
// first step is to sort activities by finish time, in order to save time,
// we use an sorted finishTime array
startTime := []int{1, 3, 1, 5, 8, 6}
finishTime := []int{2, 6, 6, 7, 10, 8}
activitySelection(startTime, finishTime)
}
// Output
// activity 1
// activity 2
// activity 5