827. Take One Two or Three

Two friends, Mira and Teo, play a game on a row of face-up cards. Each card shows an integer score, possibly negative. Mira moves first and the players alternate. On a turn the active player must take the first one, two or three cards still at the front of the row (all of them if fewer remain) and add their scores to a personal total. The game ends when the row is empty. Both players play perfectly, each trying to finish with the larger total.

Given values, the scores from front to back, return "Mira" if she ends with a strictly larger total, "Teo" if he does, and "Draw" if the totals are equal. Aim for time O(n).

Example 1

Input:
values = [5,-8,12,-4,9,-7,3]
Output:
"Mira"
Explanation:

With best play from both sides the result is Mira.

Example 2

Input:
values = [-4,-6,-1,-8,-2]
Output:
"Teo"
Explanation:

Every card hurts, so each player grabs as few as sensible; the outcome is Teo.

Example 3

Input:
values = [6,6,-12,6]
Output:
"Mira"
Explanation:

The totals can balance out exactly or tip to one side; here the outcome is Mira.

Constraints

1 ≤ values.length ≤ 105

-1000 ≤ values[i] ≤ 1000

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…