Stock After the Day
events lists what happened in a shop during one day, in order. Each event is a string "in NAME N" or "out NAME N", where NAME is a product name and N a positive whole number. The shop starts with nothing.
in adds N units of the product. out removes N units, but never more than the shop has: if only M < N units are left, those M units go and the rest of the order is dropped.
Return a dictionary mapping each product that has at least one unit left at the end to its number of units. Products at zero are left out. No events gives an empty dictionary.
Example 1
Apples: 5 in, 2 out, 3 left. Pears: 3 in, then an order for 5 takes the 3 that exist, 0 left, so pear is not in the answer.
Example 2
The two milk units come in and go out again.
Constraints
0 <= len(events) <= 1001 <= N <= 100- names are 1 to 8 lowercase letters
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Hash maps problems
- Sum of Unique ElementsEasy
- Top K Frequent ElementsEasy
- Unique Morse Code WordsEasy
- Unique OccurrencesEasy
- Widest Spread ValueEasy
- Bulls and CowsMedium
- Contiguous ArrayMedium
- Group AnagramsMedium