Python beat 99%, with line by line touch up
Read through blogs listed, but constantly stuck at "who and when to do the flip?".\nAssume I am either Alice or Bob, why and when I would do flip? \nFor some co
Read through blogs listed, but constantly stuck at "who and when to do the flip?".\nAssume I am either Alice or Bob, why and when I would do flip? \nFor some context, please refer to [here](https://leetcode.com/problems/stone-game-ix/discuss/1514529/simulation-solution) or [here](https://leetcode.com/problems/stone-game-ix/discuss/1500245/JavaC%2B%2BPython-Easy-and-Concise-6-lines-O(n)).\nThen by some rumination, found the answer to the question - whenever possible (meaning mod[0]%2 == 1), Alice will do flipping on her second shot.\nBefore explaining why, let\'s first look at case when no flipping is possible, meaning mod[0]%2 is 0, under this occasion Alice would always win by picking the mod that is less. For example, suppose we have [1,1,2,2,2], then mod[1] is 2 and mod[2] is 3, by having Alice taking 1, we will have:\n\n```\nA B A B A B\n1 1 2 ?\n```\n\nB lose the game.\nBut what if the mod that is less is zero so Alice has to pick the bigger one? \nBob would win. For example, if we have [1]:\n\n```\nA B A B\n1 \n```\nB wins by running out of options. If we have [1,1]:\n\n```\nA B A B\n1 1 \n```\nSame as above, then what if it is [1,1,1]?\n\n```\nA B A B A\n1 1 ?\n```\nThis is no 2, so A lose.\nThat\'s all for mod[0]%2 == 0.\n\nThen for mod[0]%2==1, what strategy should A take to maximize the chance of winning? The anwer is rightly the opposite - A will start with mod that is bigger. For example, if we have [3,1,1,2,2,2,2], then mod[1] is 2 and mod[2] is 4, A **must not** start with 1 because B will immeidately use flipping to prevent A\'s holding of 2 since the 3rd rd:\n\n```\nA B A B A B\n1 0 1 2 ?\n```\n\nBy B flipping at 2nd rd (and he will), A will be forced to stick to the mod that is less, and thereby bound to loss in the end. \nSo, how about letting A start with the mod that has more occurrance? \n\n```\nA B A B A B A B \n2 2 0 1 2 1 2 \n```\n\nBe taking the bigger mod and flipping at 3rd rd, A would withhold her ownership of the mod that has more occurrance and gurrantee her victory - wait ... in above case Alice still lose, why?\nBecause the mod are exhausted! So we have to assure Alice\'s success by a condition:\n\n```\nmod[more] - mod[less] >= 3\n```\n\nFor example, suppose we now have [3,1,1,2,2,2,2,2], mod[2] - mod[1] = 3, then the story would be like:\n\n```\nA B A B A B A B\n2 2 0 1 2 1 2 ?\n```\n\nNow B lose since he needs 1 but only another 2 left!\nHence we have the code:\n\n```\nclass Solution:\n    def stoneGameIX(self, stones: List[int]) -> bool:\n        mods = [0] * 3\n        for stone in stones: mods[stone%3] += 1\n        if mods[0]%2 == 0:\n            if mods[1] == 0 or mods[2] == 0: return False\n            return True\n        else:\n            if abs(mods[1]-mods[2]) >= 3: return True\n            return False\n```\n\nBefore ending the doc, why not B do flipping on his first turn even that A start with 2 for [3,1,1,2,2,2,2,2]?\nLike:\n\n```\nA B A B A B A B\n2 0 2 1 2 1 2 ?\n```\n\nSee the problem here? B\'s flipping makes little difference since A gets the winning mod(2) whatsoever, so why bother? \nFinally, if asked with this problem during an interview, don\'t think I could make it.
[]