So we have to maximize Alice’s moves and minimize Bob’s moves. But a greedy approach won’t fully suffice, simply because in a move taking the largest element between arr[0]
AND arr[-1]
won’t guarantee that Bob gets the smallest in the next round. Meaning, we would have to consider all possible routes. Hmmm smells like DP in here.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since by memoisation we reduce the duplicate calculations and each element is visited at most once.
-
Memory
The memory usage is since we use the
collections.defaultdict
object to store memoised values.
— A