Teaching Kids Programming – Passing Item From One End to Another (Who Has It After N Seconds: Math/Simulation)



Teaching Kids Programming – Passing Item From One End to Another (Who Has It After N Seconds: Math/Simulation) | ninjasquad

Teaching Kids Programming: Videos on Data Structures and Algorithms

There are n people standing in a line labeled from 1 to n. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.

For example, once the pillow reaches the nth person they pass it to the n – 1th person, then to the n – 2th person and so on.
Given the two positive integers n and time, return the index of the person holding the pillow after time seconds.

Example 1:
Input: n = 4, time = 5
Output: 2
Explanation: People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2.
Afer five seconds, the pillow is given to the 2nd person.

Example 2:
Input: n = 3, time = 2
Output: 3
Explanation: People pass the pillow in the following way: 1 -> 2 -> 3.
Afer two seconds, the pillow is given to the 3rd person.

Constraints:
2 <= n <= 1000
1 <= time <= 1000

Passing Item From One End to Another (Who Has It After N Seconds)

We can perform a simulation algorithm. Each second, we pass the item, and we change the direction when it reaches one end. The time complexity is O(T) as we need to iterate exactly t steps. O(1) constant space complexity.

1
2
3
4
5
6
7
8
9
class Solution:
    def passThePillow(self, n: int, t: int) -> int:
        x = 1
        d = 1
        for _ in range(t):
            x += d
            if x == n or x == 1:
                d *= -1
        return x
class Solution:
    def passThePillow(self, n: int, t: int) -> int:
        x = 1
        d = 1
        for _ in range(t):
            x += d
            if x == n or x == 1:
                d *= -1
        return x

For N persons, it requires (N-1)*2 times to pass it back to the first person. Thus, we can reduce the time by % operator. And then, there are two possilities, either the item is passing from the left to the right, or it is on the way back to the first person. We check which way by getting the minimum (closer to the left). This is just pure math, which has O(1) time and O(1) space.

1
2
3
4
5
6
class Solution:
    def passThePillow(self, n: int, t: int) -> int:
        T = (n - 1) * 2
        t %= T
        t += 1
        return min(t, n * 2 - t)
class Solution:
    def passThePillow(self, n: int, t: int) -> int:
        T = (n - 1) * 2
        t %= T
        t += 1
        return min(t, n * 2 - t)

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading…

535 words
Last Post: Is Ezoc Better Than Adsense?
Next Post: Teaching Kids Programming – Converting Breadth First Search Algorithm to Iterative Preorder Order (Depth First Search) for a Binary Tree





Source: Internet

Leave a Comment

We are offering free coding tuts

X