Skip to content

Commit b29755c

Browse files
solves #2582: Pass the Pillow in java
1 parent eba888a commit b29755c

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@
805805
| 2570 | [Merge Two 2D Arrays by Summing Values](https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values) | [![Java](assets/java.png)](src/MergeTwo2DArraysBySummingValues.java) | |
806806
| 2574 | [Left and Right Sum Differences](https://leetcode.com/problems/left-and-right-sum-differences) | [![Java](assets/java.png)](src/LeftAndRightSumDifferences.java) | |
807807
| 2578 | [Split With Minimum Sum](https://leetcode.com/problems/split-with-minimum-sum) | [![Java](assets/java.png)](src/SplitWithMinimumSum.java) | |
808-
| 2582 | [Pass the Pillow](https://leetcode.com/problems/pass-the-pillow) | | |
808+
| 2582 | [Pass the Pillow](https://leetcode.com/problems/pass-the-pillow) | [![Java](assets/java.png)](src/PassThePillow.java) | |
809809
| 2586 | [Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range) | | |
810810
| 2591 | [Distribute Money to Maximum Children](https://leetcode.com/problems/distribute-money-to-maximum-children) | | |
811811
| 2595 | [Number of Even and Odd Bits](https://leetcode.com/problems/number-of-even-and-odd-bits) | | |

src/PassThePillow.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// https://leetcode.com/problems/pass-the-pillow
2+
// T: O(1)
3+
// S: O(1)
4+
5+
public class PassThePillow {
6+
public int passThePillow(int n, int time) {
7+
final int direction = (int) Math.ceil((double) time / (n - 1));
8+
final int multiplier = direction % 2 == 0 ? -1 : 1;
9+
final int steps = time - (direction - 1) * (n - 1);
10+
if (multiplier == 1) {
11+
return steps + 1;
12+
}
13+
return n - steps;
14+
}
15+
}

0 commit comments

Comments
 (0)