Ice Cream Parlor

  • + 1 comment

    I think my solution has O(n) time complexity and O(n) space complexity.

    1. Loop through costs and build up hash table to map cost to ice cream type
    2. Loop through costs again and see if money-cost is in hash table and the index is different than the current index

    In Python 3 the code would look like this:

    def icecreamParlor(money, costs):
        costs_map = {}
        for i, cost in enumerate(costs):
            costs_map[cost] = i
            
        for i, cost in enumerate(costs):
            remaining=money-cost
            if remaining in costs_map:
                if costs_map[remaining] != i:
                    return sorted([i+1, costs_map[remaining]+1])