-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
43 lines (33 loc) · 926 Bytes
/
Test.java
File metadata and controls
43 lines (33 loc) · 926 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
33
34
35
36
37
38
39
40
41
42
43
import java.util.Arrays;
class Solution {
public int passThePillow(int n, int time) {
int direction = 1;
int passes = (n - 1);
if (time > passes) {
int splits[] = this.findDirectionAndPass(passes, time);
direction = splits[0];
time = splits[1];
}
return iterate(direction, n, time);
}
public int iterate(int direction, int n, int time) {
if (direction == 1) {
// forward direction
return time + 1;
} else {
// backward direction
return n - time;
}
}
public int[] findDirectionAndPass(int passes, int time) {
int index = 1;
while (time > 0) {
time = time - passes;
index++;
}
return new int[]{
index % 2 != 0 ? 1 : 0,
time
};
}
}