-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily19.cpp
More file actions
32 lines (30 loc) · 985 Bytes
/
Copy pathdaily19.cpp
File metadata and controls
32 lines (30 loc) · 985 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
28
29
30
31
32
// 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 ...
auto period = (n - 1) * 2;
auto t = time % period;
if (t < n)
return 1 + t;
else
return 1 + (period - t);
}
};
// Solution 2
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
if ((time % ((n - 1) * 2)) < n)
return 1 + (time % ((n - 1) * 2));
else
return 1 + (((n - 1) * 2) - (time % ((n - 1) * 2)));
}
};