Search - Hash Tables: Ice Cream Parlor

 Search - Hash Tables: Ice Cream Parlor

For example, there are  flavors having . Together they have  to spend. They would purchase flavor ID's  and  for a cost of . Use  based indexing for your response.

Code:

static void whatFlavors(int[] cost, int money) {

      HashMap<Integer, Integer> map = new HashMap<>();

    for(int i = 0; i < cost.length; i++){

       int target = money-cost[i];

        if(cost[i] < money){

            if(map.containsKey(target)){

                System.out.println(map.get(target) + " " + (i+1));

                break;

            }

            else{

                map.put(cost[i], i+1);

            }

        }

    }

    }


留言

這個網誌中的熱門文章

考績被打差了 輕率離職會更傷

Arrays - DS (Reverse array) [Easy]

WireMock