Elimination Game

https://leetcode.com/problems/elimination-game/

一道有趣的题。

class Solution:
    def lastRemaining(self, n: int) -> int:
        remain = n
        step = 1
        head = 1
        left = True
        while remain> 1:
            if left or remain % 2 == 1:
                head = head + step
            left = not left
            remain = remain // 2
            step *= 2
        return head
        

Last updated

Was this helpful?