Skip to content

Commit 1fbf15a

Browse files
authored
Merge pull request #926 from 0xff-dev/2582
Add solution and test-cases for problem 2582
2 parents 778d7a4 + ed84a32 commit 1fbf15a

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# [2582.Pass the Pillow][title]
2+
3+
## Description
4+
There are `n` people standing in a line labeled from `1` to `n`. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.
5+
6+
- For example, once the pillow reaches the n<sup>th</sup> person they pass it to the `n - 1th` person, then to the `n - 2th` person and so on.
7+
8+
Given the two positive integers `n` and `time`, return the index of the person holding the pillow after `time` seconds.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: n = 4, time = 5
14+
Output: 2
15+
Explanation: People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2.
16+
After five seconds, the 2nd person is holding the pillow.
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: n = 3, time = 2
23+
Output: 3
24+
Explanation: People pass the pillow in the following way: 1 -> 2 -> 3.
25+
After two seconds, the 3rd person is holding the pillow.
26+
```
27+
28+
## 结语
29+
30+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
31+
32+
[title]: https://leetcode.com/problems/pass-the-pillow
33+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, time int) int {
4+
need := n - 1
5+
6+
loop := time / need
7+
left := time % need
8+
if left != 0 {
9+
loop++
10+
}
11+
12+
if left == 0 {
13+
left = need
14+
}
15+
if loop&1 != 0 {
16+
return left + 1
17+
}
18+
return need - left + 1
519
}

leetcode/2501-2600/2582.Pass-the-Pillow/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
a, b int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 4, 5, 2},
17+
{"TestCase2", 3, 2, 3},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.a, c.b)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.a, c.b)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)