File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed
Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+
4+ class Solution {
5+ public int solution (int [] picks , String [] minerals ) {
6+ int answer = 0 ;
7+
8+ int totalPicks = picks[0 ] + picks[1 ] + picks[2 ];
9+ int limit = Math . min(minerals. length, totalPicks * 5 );
10+
11+ PriorityQueue<int[]> pq = new PriorityQueue<> ((a, b) - > {
12+ if (b[0 ] != a[0 ]) return b[0 ] - a[0 ];
13+ if (b[1 ] != a[1 ]) return b[1 ] - a[1 ];
14+ return b[2 ] - a[2 ];
15+ });
16+
17+ int dia = 0 , iron = 0 , stone = 0 , cnt = 0 ;
18+
19+ for (int i = 0 ; i < limit; i++ ) {
20+ String m = minerals[i];
21+ if (m. equals(" diamond" )) {
22+ dia += 1 ; iron += 5 ; stone += 25 ;
23+ } else if (m. equals(" iron" )) {
24+ dia += 1 ; iron += 1 ; stone += 5 ;
25+ } else {
26+ dia += 1 ; iron += 1 ; stone += 1 ;
27+ }
28+ cnt++ ;
29+
30+ if (cnt == 5 ) {
31+ pq. offer(new int []{stone, iron, dia});
32+ dia = iron = stone = 0 ;
33+ cnt = 0 ;
34+ }
35+ }
36+ if (cnt > 0 ) {
37+ pq. offer(new int []{stone, iron, dia});
38+ }
39+
40+ while (! pq. isEmpty() && (picks[0 ] + picks[1 ] + picks[2 ] > 0 )) {
41+ int [] g = pq. poll();
42+ if (picks[0 ] > 0 ) {
43+ answer += g[2 ];
44+ picks[0 ]-- ;
45+ } else if (picks[1 ] > 0 ) {
46+ answer += g[1 ];
47+ picks[1 ]-- ;
48+ } else { answer += g[0 ];
49+ picks[2 ]-- ;
50+ }
51+ }
52+
53+ return answer;
54+ }
55+ }
56+
57+ ```
You can’t perform that action at this time.
0 commit comments