-
Notifications
You must be signed in to change notification settings - Fork 0
/
1235. Maximum Profit in Job Scheduling
47 lines (39 loc) · 1.21 KB
/
1235. Maximum Profit in Job Scheduling
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
38
39
40
41
42
43
44
45
46
47
class Solution {
public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
int size = startTime.length;
Myobject arr[] = new Myobject[size];
for (int i = 0; i < size; i++) {
arr[i] = new Myobject(startTime[i], endTime[i], profit[i]);
}
Arrays.sort(arr, (a, b) -> Integer.compare(a.end, b.end));
int dp[] = new int[size+1];
for (int i = 0; i < size; i++) {
int search = arr[i].start;
int Idx = binarySearch(arr, search, i);
dp[i + 1] = Math.max(dp[i], dp[Idx] + arr[i].provit);
}
return dp[size];
}
int binarySearch(Myobject[] arr, int target, int i) {
int left = 0;
int right = i;
while (left < right) {
int mid = (left + right) / 2;
if (arr[mid].end <= target)
left = mid + 1;
else
right = mid;
}
return left;
}
}
class Myobject {
int end;
int start ;
int provit;
public Myobject(int e1, int e2, int e3) {
this.start = e1;
this.end = e2;
this.provit = e3;
}
}