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.
My Java solution with o(n log n) time complexity and o(1) space complexity:
publicstaticinttoys(List<Integer>w){// goal: determine the min amt of subarrays that can be created based on the container ruleif(w.size()==1)return1;//sort arrCollections.sort(w);//iterate over each val until it reaches the max container val and increment the container countintmaxContainerVal=w.get(0)+4;intcontainers=1;for(inti=0;i<w.size();i++){if(w.get(i)>maxContainerVal){maxContainerVal=w.get(i)+4;containers++;}}returncontainers;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Priyanka and Toys
You are viewing a single comment's thread. Return to all comments →
My Java solution with o(n log n) time complexity and o(1) space complexity: