Hash Tables: Ice Cream Parlor

  • + 1 comment

    Here's my Java 8 solution, passing all tests, for Max Score of 35:

    class Result {
        
        private static class IceCreamIdCostBinding {
    
            private int id;
            private int cost;
    
            // Constructor
            public IceCreamIdCostBinding(int id, int cost) {
                this.id = id;
                this.cost = cost;
            }
    
            // Getter for id
            public int getId() {
                return id;
            }
    
            // Getter for cost
            public int getCost() {
                return cost;
            }
            
            public String toString() {
                return "{ id = >" + id + "< | cost = >" + cost + "< }";
            }
        }
    
        /*
         * Complete the 'whatFlavors' function below.
         *
         * The function accepts following parameters:
         *  1. INTEGER_ARRAY cost
         *  2. INTEGER money
         */
    
        public static void whatFlavors(List<Integer> cost, int money) {
            // Write your code here
            List<IceCreamIdCostBinding> iceCreamIdCostBindingsLst = new ArrayList<>();
            for (int cIdx = 0 ; cIdx < cost.size() ; cIdx++) {
                iceCreamIdCostBindingsLst.add(new IceCreamIdCostBinding(
                                                      (cIdx + 1), cost.get(cIdx)));
            }
    
            iceCreamIdCostBindingsLst.sort(
                Comparator.comparingInt(IceCreamIdCostBinding::getCost));
    
            int sortedArrEndIdx = 
                Collections.binarySearch(
                    iceCreamIdCostBindingsLst,
                    new IceCreamIdCostBinding(0, money),
                    Comparator.comparingInt(IceCreamIdCostBinding::getCost));
    
            // If value not found, binarySearch returns (-(insertion point) - 1)
            if (sortedArrEndIdx < 0) {
                sortedArrEndIdx = (iceCreamIdCostBindingsLst.size() - 1);
            } else {
                sortedArrEndIdx--;
            }
            
            for (int cIdx = 0 ; cIdx < sortedArrEndIdx ; cIdx++) {
                int remainingMoney = 
                    (money - iceCreamIdCostBindingsLst.get(cIdx).getCost());
    
                List<IceCreamIdCostBinding> searchSubLst = 
                    iceCreamIdCostBindingsLst.subList((cIdx + 1), (sortedArrEndIdx + 1));
                
                int remMoneySubLstIdx = 
                    Collections.binarySearch(
                        searchSubLst,
                        new IceCreamIdCostBinding(0, remainingMoney),
                        Comparator.comparingInt(IceCreamIdCostBinding::getCost));
    
                if (remMoneySubLstIdx >= 0) {
                    int iceCreamID1 = 
                        iceCreamIdCostBindingsLst.get(cIdx).getId();
                    int iceCreamID2 = 
                        searchSubLst.get(remMoneySubLstIdx).getId();
    
                    System.out.println(Math.min(iceCreamID1, iceCreamID2) + 
                                       " " + Math.max(iceCreamID1, iceCreamID2));
    
                    break;
                }
            }
            
        }
    
    }