-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily18.cpp
More file actions
27 lines (26 loc) · 746 Bytes
/
Copy pathdaily18.cpp
File metadata and controls
27 lines (26 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Solution 1
class Solution {
public:
int passThePillow(int n, int time) {
// if time is 1x, 3x, 5x n then it's going the original direction
// if time is 2x, 4x, 6x n then it's going in the opposite direction
// 1 2 3 4 - 5
// (1) 2 3 4 3 2 1 | 2 3 4 3 2 1 | 2 3 4 3 ... cycles of n - 1 * 2
auto period = (n - 1) * 2;
auto t = time % period;
if (t < n)
return 1 + t;
else
return 1 + (period - t);
}
};
// OR
class Solution {
public:
int passThePillow(int n, int time) {
if ((time % ((n - 1) * 2)) < n)
return 1 + (time % ((n - 1) * 2));
else
return 1 + (((n - 1) * 2) - (time % ((n - 1) * 2)));
}
}