Hash Tables: Ice Cream Parlor

Sort by

recency

|

671 Discussions

|

  • + 0 comments
    public static void whatFlavors(List<Integer> cost, int money) {
    // Write your code here
    Map<Integer, List<Integer>> costByIndex = new HashMap<>();
    for(int i =0;i<cost.size();i++){
        int balance = money-cost.get(i);
        List<Integer> ids;
        if(costByIndex.containsKey(balance)){
            ids = costByIndex.get(balance);
        }else ids = new ArrayList<>();
        ids.add(i+1);
        costByIndex.put(balance, ids);
    }
    
    
     for(int i =0;i<cost.size();i++){
        int balance = cost.get(i);
         if(costByIndex.containsKey(balance)&& (costByIndex.get(balance).get(0)!=(i+1))){
            int val1 = Math.min(i+1,costByIndex.get(balance).get(0));
            int val2 = Math.max(i+1,costByIndex.get(balance).get(0));
            System.out.println(val1+" "+val2);
            break;
        }
    }
    
  • + 0 comments

    JS - only using array

    function whatFlavors(cost, money) {
      // Write your code here
      for (let i = 0; i < cost.length; i++) {
        const flavor1 = cost[i];
        if (flavor1 < money) {
            // only check indexes after the current one (i)
            const idxFlavor2 = cost.indexOf(money - flavor1, i + 1);
            
            // check if "corresponding" price exists, if true: print and exit
            if (idxFlavor2 > i) {
                console.log(i + 1 + ' ' + (idxFlavor2 + 1));
                // we done, loop can be aborted
                return;
            }
        }
      }
    }
    
  • + 0 comments

    JAVA Solution:

        int[] arr=cost.stream().mapToInt(Integer::intValue).toArray();
    Arrays.sort(arr);
    int start=0;
    int end=arr.length-1;
    while(start<end){
        if(arr[start]+arr[end]>money){
            end--;
        }
        else if(arr[start]+arr[end]<money){
            start++;
        }
        else{
            TreeSet<Integer> res=new TreeSet<>();
            res.add(cost.indexOf(arr[start])+1);
            int pos2=cost.indexOf(arr[end])+1;;
            if(arr[start]==arr[end]){
                cost.remove(cost.indexOf(arr[start]));
            pos2=cost.indexOf(arr[end])+2;
            }
            res.add(pos2);
    
    
            System.out.print(res.first()+" "+res.last());
            System.out.println();
            break;
        }
    }
    
  • + 0 comments

    C++ Solution

    void whatFlavors(vector<int> cost, int money) 
    {
        map<int, int> hash;
        for(int i = 0; i < cost.size(); i++)
        {
            int remaining_money = money - cost[i];
            if(remaining_money <= 0) continue; // cant buy a second flavor so continues
            if(hash[remaining_money] > 0) //if it exists
            {
                cout << hash[remaining_money] << " " << i + 1 << endl;
                return;
            }
            hash[cost[i]] = i + 1;
        }
    }
    
  • + 0 comments

    Python3

    def whatFlavors(cost, money):
        mapping = {}
        
        for i in range(len(cost)):
            current_price = cost[i]
            if current_price in mapping.keys():
                if mapping[current_price] < i + 1:
                    # Print complement price index first
                    print("{} {}".format(mapping[current_price], i + 1))
                else:
                    print("{} {}".format(i + 1, mapping[cost[i]]))
            # Map current price index to complement price
            mapping[money - current_price] = i + 1