File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+
4+ class Solution {
5+ private static HashMap<String , Integer > wordsIndex;
6+
7+ public int solution (String [] wants , int [] number , String [] discounts ) {
8+ int answer = 0 ;
9+ wordsIndex = new HashMap<String , Integer > ();
10+ int i = 0 ;
11+ for (String want : wants){
12+ wordsIndex. put(want, i++ );
13+ }
14+ int [] counts = new int [wants. length];
15+
16+ ArrayDeque<String > q = new ArrayDeque<String > ();
17+ String polled;
18+ for (String discount : discounts){
19+ if (wordsIndex. containsKey(discount)){
20+ counts[wordsIndex. get(discount)]++ ;
21+ }
22+ q. offer(discount);
23+ if (q. size() > 10 ){
24+ polled = q. poll();
25+ if (wordsIndex. containsKey(polled)){
26+ counts[wordsIndex. get(polled)]-- ;
27+ }
28+ }
29+
30+ if (q. size() == 10 ){
31+ boolean ok = true ;
32+ for (int k= 0 ; k< number. length; k++ ){
33+ if (counts[k] < number[k]){
34+ ok = false ;
35+ break ;
36+ }
37+ }
38+ if (ok){
39+ answer++ ;
40+ }
41+ }
42+ }
43+
44+ return answer;
45+ }
46+ }
47+
48+ ```
You can’t perform that action at this time.
0 commit comments