-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion6.java
More file actions
39 lines (35 loc) · 1.14 KB
/
Copy pathQuestion6.java
File metadata and controls
39 lines (35 loc) · 1.14 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
package practice4_sort;
import java.util.Arrays;
public class Question6 {
public int solution(int[] tasks, long k) {
int answer = 0;
int[] sT = new int[tasks.length + 1];
System.arraycopy(tasks, 0, sT, 1, tasks.length);
Arrays.sort(sT);
int rest = tasks.length;
for(int i = 1; i < sT.length; i++){
long time = ((long)rest * (sT[i] - sT[i-1]));
if(k < time){
long idx = k % rest;
int cnt = 0;
for(int j = 0; j < tasks.length; j++){
if(tasks[j] >= sT[i]){
if(cnt == idx) return j+1;
cnt++;
}
}
}
else{
k -= time;
rest--;
}
}
return -1;
}
public static void main(String[] args){
Question6 T = new Question6();
System.out.println(T.solution(new int[]{1, 2, 3}, 5));
System.out.println(T.solution(new int[]{8, 5, 2, 9, 10, 7}, 30));
System.out.println(T.solution(new int[]{8, 9, 12, 23, 45, 16, 25, 50}, 100));
}
}