We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
in java.
* the first hashmap h tracks the integers and their count.
* the second hashmap o tracks the counts and occurrences of the counts.
* test-11 requires that second hashmap. this is because the getValue method is O(n) if you tried to found counts if you only had hashmap o.
// Complete the freqQuery function below.staticList<Integer>freqQuery(List<List<Integer>>queries){List<Integer>ls=newArrayList<>();HashMap<Integer,Integer>h=newHashMap<>();HashMap<Integer,Integer>o=newHashMap<>();for(inti=0;i<queries.size();i++){inta=queries.get(i).get(0);intb=queries.get(i).get(1);intv=0;// a is choice; b is int; v is count of int; get(v) is occurances of that countswitch(a){case1://below is work for countsif(h.containsKey(b)){v=h.get(b);h.replace(b,v,v+1);}else{h.put(b,1);v=0;}//below is work for occurences of countsif(o.containsKey(v)&&o.get(v)>0)o.replace(v,o.get(v),o.get(v)-1);v++;//because we're increasingif(o.containsKey(v))o.replace(v,o.get(v),o.get(v)+1);elseo.put(v,1);break;case2:if(h.containsKey(b)){v=h.get(b);if(v>1){h.replace(b,v,v-1);}elseh.remove(b);if(o.containsKey(v)&&o.get(v)>0)o.replace(v,o.get(v),o.get(v)-1);v--;//because we're reducingif(o.containsKey(v))o.replace(v,o.get(v),o.get(v)+1);}break;case3:if(o.containsKey(b)&&o.get(b)>0)ls.add(1);elsels.add(0);break;}}returnls;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Frequency Queries
You are viewing a single comment's thread. Return to all comments →
in java. * the first hashmap h tracks the integers and their count. * the second hashmap o tracks the counts and occurrences of the counts. * test-11 requires that second hashmap. this is because the getValue method is O(n) if you tried to found counts if you only had hashmap o.