File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ class Solution {
4
+ public class Food implements Comparable <Food > {
5
+ int index , time ;
6
+
7
+ Food (int index , int time ) {
8
+ this .index = index ;
9
+ this .time = time ;
10
+ }
11
+
12
+ @ Override
13
+ public int compareTo (Food o ) {
14
+ return Integer .compare (this .time , o .time );
15
+ }
16
+ }
17
+
18
+ public int solution (int [] food_times , long k ) {
19
+ long sum = 0 ;
20
+
21
+ PriorityQueue <Food > queue = new PriorityQueue <>();
22
+
23
+ for (int i = 0 ; i < food_times .length ; i ++) {
24
+ queue .add (new Food (i , food_times [i ]));
25
+ }
26
+
27
+ int prevTime = 0 ;
28
+
29
+ while (!queue .isEmpty ()) {
30
+ int size = queue .size ();
31
+ Food cur = queue .peek ();
32
+
33
+ long cost = (long ) size * (cur .time - prevTime );
34
+ if (sum + cost <= k ) {
35
+ sum += cost ;
36
+ prevTime = cur .time ;
37
+ queue .poll ();
38
+ } else {
39
+ k -= sum ;
40
+ break ;
41
+ }
42
+ }
43
+
44
+ if (queue .isEmpty ()) return -1 ;
45
+
46
+ List <Food > list = new ArrayList <>();
47
+ while (!queue .isEmpty ()) {
48
+ list .add (queue .poll ());
49
+ }
50
+
51
+ list .sort (Comparator .comparingInt (o -> o .index ));
52
+
53
+ return list .get ((int ) (k % list .size ())).index + 1 ;
54
+ }
55
+ }
You can’t perform that action at this time.
0 commit comments