Skip to content

Commit 7495bc0

Browse files
Solve daily problem
1 parent ac7c406 commit 7495bc0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Intuition
2+
similiar to solve 3178. Find the Child Who Has the Ball After K Seconds
3+
4+
Approach
5+
Modular arithmetic with period N=2*(n-1)
6+
7+
Modular arithmetic x=time%N
8+
The answer is 1+((x<n)?x:N-x)
9+
Let us consider the small integer, say n=5
10+
1 2 3 4 5
11+
9 8 7 6
12+
10 11 12 13
13+
17 16 15 14
14+
N=2×5-2=8, when x>=n, x=time mod N one has to do N-x. Don't forget plus 1.
15+
16+
Complexity
17+
Time complexity:
18+
O(1)
19+
20+
Space complexity:
21+
O(1)

188-6th_July-Pass_the_pillow/code.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution
2+
{
3+
public:
4+
int passThePillow(int n, int time)
5+
{
6+
int N = 2 * n - 2, x = time % N;
7+
return 1 + ((x < n) ? x : N - x);
8+
}
9+
};

0 commit comments

Comments
 (0)