2582. Pass the Pillow

Photo by Kiran CK on Unsplash

2582. Pass the Pillow

Using simulation

class Solution:
    def passThePillow(self, n: int, time: int) -> int:
        person = 1
        mod = 1 
        while time > 0:
            time -= 1
            person += mod
            if person == n:
                mod = -1            
            if person == 1:
                mod = 1
        return person

Using Math

class Solution:
    def passThePillow(self, n: int, time: int) -> int:
        timeToReachEnd = n -  1
        rem = time % timeToReachEnd
        trip = time // timeToReachEnd

        if trip % 2:
            return n - rem
        else:
            return rem + 1