2582. Pass the Pillow
UnknownView on LeetCode
Time: O(1)
Space: O(1)
Problem Overview
Pass the Pillow (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Simulation pattern in coding interviews. Compute position via modular arithmetic on zigzag period of 2*(n-1).
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Compute position via modular arithmetic on zigzag period of 2*(n-1).
Related patterns: Array, Simulation
2582.cs
C#
// Approach: Compute position via modular arithmetic on zigzag period of 2*(n-1).
// Time: O(1) Space: O(1)
public class Solution
{
public int PassThePillow(int n, int time)
{
time %= (n - 1) * 2;
if (time < n)
return time + 1;
return n - (time - (n - 1));
}
}Was this solution helpful?
Related Problems
- 498. Diagonal Traverse(Medium)
- 799. Champagne Tower(Medium)
- 832. Flipping an Image(Easy)
- 838. Push Dominoes(Medium)
- 874. Walking Robot Simulation(Medium)
- 885. Spiral Matrix III(Medium)