> For the complete documentation index, see [llms.txt](https://leetcodebook-1.gitbook.io/top-interview-150/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://leetcodebook-1.gitbook.io/top-interview-150/array-string/candy.md).

# 分发糖果

题目链接: <https://leetcode.cn/problems/candy>

## 解题思路(与官解一致)：

1. 题目要求每个小孩必须至少一颗糖果，且相邻的两个小孩评分高的拿的更多
2. 因此分糖果的结果需要满足一个条件，无论从左到右遍历还是从右到左遍历，都满足一个条件，当前这个小孩与前一个小孩相比如果评分更高则拿的多，评分低则拿的少
3. 为了能得到最小值，则，当前小孩比前一个小孩评分低时直接拿最少1颗
4. 在从左往右分配及从右往左分配两个方向中每个小孩能拿到的最多的糖果数量即可满足上述条件

```go
func candy(ratings []int) int {
	length := len(ratings)
	candyList := make([]int, length)
	for idx, item := range ratings {
		if idx > 0 && item > ratings[idx-1] {
			candyList[idx] = candyList[idx-1] + 1
		} else {
			candyList[idx] = 1
		}
	}
	last, count := 0, 0
	for i := length - 1; i >= 0; i-- {
		if i < length-1 && ratings[i] > ratings[i+1] {
			last++
		} else {
			last = 1
		}
		if last > candyList[i] {
			count += last
		} else {
			count += candyList[i]
		}
	}
	return count
}
```

## 复杂度分析

* **时间复杂度：** 对数组进行了单轮遍历，因此时间复杂度为 $$O(n)$$，其中 $$n$$ 是数组 $$ratings$$ 的长度
* **空间复杂度：** 空间复杂度为 $$O(n)$$


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://leetcodebook-1.gitbook.io/top-interview-150/array-string/candy.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
