-
Notifications
You must be signed in to change notification settings - Fork 481
/
1235.go
37 lines (35 loc) · 902 Bytes
/
1235.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
36
37
func jobScheduling(startTime []int, endTime []int, profit []int) int {
n := len(startTime)
jobs := make([][]int, 0)
for i := 0; i < n; i++ {
jobs = append(jobs, []int{startTime[i], endTime[i], profit[i]})
}
sort.Slice(jobs, func(i, j int) bool {
return jobs[i][1] < jobs[j][1]
})
dp := make([]int, n)
dp[0] = jobs[0][2]
for i := 1; i < n; i++ {
l, r := 0, i - 1
for l < r {
mid := (l + r + 1) >> 1
if jobs[mid][1] <= jobs[i][0] {
l = mid
} else {
r = mid - 1
}
}
if jobs[l][1] <= jobs[i][0] {
dp[i] = max(dp[i - 1], dp[l] + jobs[i][2]);
} else {
dp[i] = max(dp[i - 1], jobs[i][2]);
}
}
return dp[n - 1]
}
func max(a, b int) int {
if a > b {
return a
}
return b
}