Ice Cream Parlor

  • + 0 comments

    o(n) time, o(n) space My Java solution:

    public static List<Integer> icecreamParlor(int m, List<Integer> arr) {
            //goal: determine two indexes from arr that equal m when added together
            //use a map to store value and index
            Map<Integer, Integer> valueMap = new HashMap<>();
            for(int i = 0; i < arr.size(); i++){
                int price = arr.get(i); //current price
                int complement = m - price; // other price needed to equal m
                if(valueMap.containsKey(complement)){
                    List<Integer> indices = Arrays.asList(valueMap.get(complement) + 1, i + 1); // 1 based index
                    Collections.sort(indices);
                    return indices;
                } else{
                    valueMap.put(price, i); //value map didnt contain complement
                } 
            }
            return List.of(); //no valid pair was found
        }