HackerRank Python - collections.Counter()



collections.Counter()
A counter is a container that stores elements as dictionary keys, and their counts are stored as dictionary values.



  1. # Enter your code here. Read input from STDIN. Print output to STDOUT
  2. from collections import Counter
  3. num_shoes = int(input())
  4. shoe_sizes = Counter(map(int, input().split()))
  5. num_customers = int(input())
  6. cost = 0
  7. for _ in range(num_customers):
  8. size, price = map(int, input().split())
  9. if shoe_sizes[size]:
  10. cost += price
  11. shoe_sizes[size] -= 1
  12. print(cost)

download android app