Use
unordered_map<char, int>
to count frequencies of balls inhand
. Use anotherunordered_map<string, int>
to memoization ofsteps
needed to emptyboard
.Use recursive DFS to try inserting every ball in hand at every position on the board.
Decrease freq of ball when inserting it on the board and increase it before trying a new combination.
Update the board after inserting a ball on the board and recursively call DFS on the new board with the new hand.
Return the minimum steps after trying all combination.
Last updated