Optimizing lookups from array to dictionary to solve time limit exceeded