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 PinSystem {
4+ class Pin {
5+ int id ;
6+ double score ;
7+
8+ public Pin (int id , double score ) {
9+ this .id = id ;
10+ this .score = score ;
11+ }
12+ }
13+
14+ Map <String , PriorityQueue <Pin >> hm ;
15+ Set <Integer > usedIds ;
16+
17+ public PinSystem () {
18+ hm = new HashMap <>();
19+ usedIds = new HashSet <>();
20+ }
21+
22+ public boolean addPin (int id , double score , String type ) {
23+ if (usedIds .contains (id )) {
24+ return false ;
25+ }
26+
27+ hm .putIfAbsent (type , new PriorityQueue <Pin >((p1 , p2 ) -> Double .compare (p2 .score , p1 .score )));
28+ hm .get (type ).offer (new Pin (id , score ));
29+ return usedIds .add (id );
30+ }
31+
32+ public List <Integer > getTopK (String type , int k ) {
33+ if (!hm .containsKey (type )) {
34+ return Collections .emptyList ();
35+ }
36+
37+ PriorityQueue <Pin > pins = hm .get (type );
38+ List <Pin > polledPins = new ArrayList <>();
39+ List <Integer > result = new ArrayList <>(k );
40+
41+ k = Math .min (k , pins .size ());
42+
43+ while (k > 0 ) {
44+ polledPins .add (pins .poll ());
45+ --k ;
46+ }
47+
48+ for (Pin p : polledPins ) {
49+ result .add (p .id );
50+ pins .offer (p );
51+ }
52+
53+ return result ;
54+ }
55+ }
You can’t perform that action at this time.
0 commit comments