Skip to content

Commit 38ffcbc

Browse files
committed
Add solution 0135
1 parent d5c5284 commit 38ffcbc

25 files changed

+408
-181
lines changed

README.md

Lines changed: 117 additions & 113 deletions
Large diffs are not rendered by default.

leetcode/0135.Candy/135. Candy.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leetcode
2+
3+
func candy(ratings []int) int {
4+
candies := make([]int, len(ratings))
5+
for i := 1; i < len(ratings); i++ {
6+
if ratings[i] > ratings[i-1] {
7+
candies[i] += candies[i-1] + 1
8+
}
9+
}
10+
for i := len(ratings) - 2; i >= 0; i-- {
11+
if ratings[i] > ratings[i+1] && candies[i] <= candies[i+1] {
12+
candies[i] = candies[i+1] + 1
13+
}
14+
}
15+
total := 0
16+
for _, candy := range candies {
17+
total += candy + 1
18+
}
19+
return total
20+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question135 struct {
9+
para135
10+
ans135
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para135 struct {
16+
ratings []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans135 struct {
22+
one int
23+
}
24+
25+
func Test_Problem135(t *testing.T) {
26+
27+
qs := []question135{
28+
29+
{
30+
para135{[]int{1, 0, 2}},
31+
ans135{5},
32+
},
33+
34+
{
35+
para135{[]int{1, 2, 2}},
36+
ans135{4},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 135------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans135, q.para135
44+
fmt.Printf("【input】:%v 【output】:%v\n", p, candy(p.ratings))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}

leetcode/0135.Candy/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# [135. Candy](https://leetcode.com/problems/candy/)
2+
3+
4+
## 题目
5+
6+
There are `n` children standing in a line. Each child is assigned a rating value given in the integer array `ratings`.
7+
8+
You are giving candies to these children subjected to the following requirements:
9+
10+
- Each child must have at least one candy.
11+
- Children with a higher rating get more candies than their neighbors.
12+
13+
Return *the minimum number of candies you need to have to distribute the candies to the children*.
14+
15+
**Example 1:**
16+
17+
```
18+
Input: ratings = [1,0,2]
19+
Output: 5
20+
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: ratings = [1,2,2]
27+
Output: 4
28+
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
29+
The third child gets 1 candy because it satisfies the above two conditions.
30+
```
31+
32+
**Constraints:**
33+
34+
- `n == ratings.length`
35+
- `1 <= n <= 2 * 10^4`
36+
- `0 <= ratings[i] <= 2 * 10^4`
37+
38+
## 题目大意
39+
40+
老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。你需要按照以下要求,帮助老师给这些孩子分发糖果:
41+
42+
- 每个孩子至少分配到 1 个糖果。
43+
- 评分更高的孩子必须比他两侧的邻位孩子获得更多的糖果。
44+
45+
那么这样下来,老师至少需要准备多少颗糖果呢?
46+
47+
## 解题思路
48+
49+
- 本题的突破口在于,评分更高的孩子必须比他两侧的邻位孩子获得更多的糖果,这句话。这个规则可以理解为 2 条规则,想象成按身高排队,站在下标为 0 的地方往后“看”,评分高即为个子高的,应该比前面个子矮(评分低)的分到糖果多;站在下标为 n - 1 的地方往后“看”,评分高即为个子高的,同样应该比前面个子矮(评分低)的分到糖果多。你可能会有疑问,规则都是一样的,为什么会出现至少需要多少糖果呢?因为可能出现评分一样高的同学。扫描数组两次,处理出每一个学生分别满足左规则或右规则时,最少需要被分得的糖果数量。每个人最终分得的糖果数量即为这两个数量的最大值。两次遍历结束,将所有糖果累加起来即为至少需要准备的糖果数。由于每个人至少分配到 1 个糖果,所以每个人糖果数再加一。
50+
51+
## 代码
52+
53+
```go
54+
package leetcode
55+
56+
func candy(ratings []int) int {
57+
candies := make([]int, len(ratings))
58+
for i := 1; i < len(ratings); i++ {
59+
if ratings[i] > ratings[i-1] {
60+
candies[i] += candies[i-1] + 1
61+
}
62+
}
63+
for i := len(ratings) - 2; i >= 0; i-- {
64+
if ratings[i] > ratings[i+1] && candies[i] <= candies[i+1] {
65+
candies[i] = candies[i+1] + 1
66+
}
67+
}
68+
total := 0
69+
for _, candy := range candies {
70+
total += candy + 1
71+
}
72+
return total
73+
}
74+
```

website/content/ChapterFour/0100~0199/0131.Palindrome-Partitioning.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,5 @@ func isPal(str string, s, e int) bool {
124124
----------------------------------------------
125125
<div style="display: flex;justify-content: space-between;align-items: center;">
126126
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0100~0199/0130.Surrounded-Regions/">⬅️上一页</a></p>
127-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0100~0199/0136.Single-Number/">下一页➡️</a></p>
127+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0100~0199/0135.Candy/">下一页➡️</a></p>
128128
</div>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [135. Candy](https://leetcode.com/problems/candy/)
2+
3+
4+
## 题目
5+
6+
There are `n` children standing in a line. Each child is assigned a rating value given in the integer array `ratings`.
7+
8+
You are giving candies to these children subjected to the following requirements:
9+
10+
- Each child must have at least one candy.
11+
- Children with a higher rating get more candies than their neighbors.
12+
13+
Return *the minimum number of candies you need to have to distribute the candies to the children*.
14+
15+
**Example 1:**
16+
17+
```
18+
Input: ratings = [1,0,2]
19+
Output: 5
20+
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: ratings = [1,2,2]
27+
Output: 4
28+
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
29+
The third child gets 1 candy because it satisfies the above two conditions.
30+
```
31+
32+
**Constraints:**
33+
34+
- `n == ratings.length`
35+
- `1 <= n <= 2 * 10^4`
36+
- `0 <= ratings[i] <= 2 * 10^4`
37+
38+
## 题目大意
39+
40+
老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。你需要按照以下要求,帮助老师给这些孩子分发糖果:
41+
42+
- 每个孩子至少分配到 1 个糖果。
43+
- 评分更高的孩子必须比他两侧的邻位孩子获得更多的糖果。
44+
45+
那么这样下来,老师至少需要准备多少颗糖果呢?
46+
47+
## 解题思路
48+
49+
- 本题的突破口在于,评分更高的孩子必须比他两侧的邻位孩子获得更多的糖果,这句话。这个规则可以理解为 2 条规则,想象成按身高排队,站在下标为 0 的地方往后“看”,评分高即为个子高的,应该比前面个子矮(评分低)的分到糖果多;站在下标为 n - 1 的地方往后“看”,评分高即为个子高的,同样应该比前面个子矮(评分低)的分到糖果多。你可能会有疑问,规则都是一样的,为什么会出现至少需要多少糖果呢?因为可能出现评分一样高的同学。扫描数组两次,处理出每一个学生分别满足左规则或右规则时,最少需要被分得的糖果数量。每个人最终分得的糖果数量即为这两个数量的最大值。两次遍历结束,将所有糖果累加起来即为至少需要准备的糖果数。由于每个人至少分配到 1 个糖果,所以每个人糖果数再加一。
50+
51+
## 代码
52+
53+
```go
54+
package leetcode
55+
56+
func candy(ratings []int) int {
57+
candies := make([]int, len(ratings))
58+
for i := 1; i < len(ratings); i++ {
59+
if ratings[i] > ratings[i-1] {
60+
candies[i] += candies[i-1] + 1
61+
}
62+
}
63+
for i := len(ratings) - 2; i >= 0; i-- {
64+
if ratings[i] > ratings[i+1] && candies[i] <= candies[i+1] {
65+
candies[i] = candies[i+1] + 1
66+
}
67+
}
68+
total := 0
69+
for _, candy := range candies {
70+
total += candy + 1
71+
}
72+
return total
73+
}
74+
```
75+
76+
77+
----------------------------------------------
78+
<div style="display: flex;justify-content: space-between;align-items: center;">
79+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0100~0199/0131.Palindrome-Partitioning/">⬅️上一页</a></p>
80+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0100~0199/0136.Single-Number/">下一页➡️</a></p>
81+
</div>

website/content/ChapterFour/0100~0199/0136.Single-Number.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ func singleNumber(nums []int) int {
4848

4949
----------------------------------------------
5050
<div style="display: flex;justify-content: space-between;align-items: center;">
51-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0100~0199/0131.Palindrome-Partitioning/">⬅️上一页</a></p>
51+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0100~0199/0135.Candy/">⬅️上一页</a></p>
5252
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0100~0199/0137.Single-Number-II/">下一页➡️</a></p>
5353
</div>

0 commit comments

Comments
 (0)