Use queue and hashmap, AC