-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivity Selection.java
More file actions
57 lines (49 loc) · 1.7 KB
/
Activity Selection.java
File metadata and controls
57 lines (49 loc) · 1.7 KB
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
48
49
50
51
52
53
54
55
56
57
//{ Driver Code Starts
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(reader.readLine().trim());
while (t-- > 0) {
// Read the start times
String[] startInput = reader.readLine().trim().split("\\s+");
int[] start = new int[startInput.length];
for (int i = 0; i < startInput.length; i++) {
start[i] = Integer.parseInt(startInput[i]);
}
// Read the end times
String[] endInput = reader.readLine().trim().split("\\s+");
int[] finish = new int[endInput.length];
for (int i = 0; i < endInput.length; i++) {
finish[i] = Integer.parseInt(endInput[i]);
}
// Create solution object and call activitySelection
Solution obj = new Solution();
System.out.println(obj.activitySelection(start, finish));
System.out.println("~");
}
}
}
// } Driver Code Ends
class Solution {
public int activitySelection(int[] start, int[] finish) {
// code here.
int n=start.length;
int[][] arr = new int[n][2];
for (int i = 0; i < n; i++) {
arr[i][0] = finish[i];
arr[i][1] = start[i];
}
Arrays.sort(arr,(a, b) -> Integer.compare(a[0], b[0]));
int fin=-1;
int ans=0;
for(int i=0;i<arr.length;i++){
if(arr[i][1]>fin){
ans++;
fin=arr[i][0];
}
}
return ans;
}
}