2582. Pass the Pillow
UnknownView on LeetCode
Time: O(1)
Space: O(1)
Advertisement
Approach
Compute position via modular arithmetic on zigzag period of 2*(n-1).
Key Techniques
Array
Array problems involve manipulating elements stored in a contiguous block of memory. Key techniques include two-pointer traversal, prefix sums, sliding windows, and in-place partitioning. In C#, arrays are zero-indexed and fixed in size — use List<T> when you need dynamic resizing.
Simulation
Simulation problems require implementing the described process step by step. Focus on correctly handling edge cases and state transitions. Common in geometry, game problems, and string manipulation. Optimize only if the naive simulation exceeds the time limit.
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));
}
}Advertisement
Was this solution helpful?