---
# System prepended metadata

title: '**Leetcode筆記(Candy)**'
tags: [hard, arrays, leetcode]

---

# **Leetcode筆記(Candy)**


:::info
:information_source: 題目 : Candy, 類型 : array , 等級 : hard
日期 : 2023/10/15
:::


### 嘗試
```python

```

---
### **優化**
![](https://hackmd.io/_uploads/H14S4k5Wp.png =60%x)

```python
class Solution:
    def candy(self, ratings: List[int]) -> int:
        arr = [1] * len(ratings)

        for i in range(1, len(ratings)):
            if ratings[i - 1] < ratings[i]:
                arr[i] = arr[i - 1] + 1
    
        for i in range(len(ratings) - 2, -1, -1):
            if ratings[i] > ratings[i + 1]:
                arr[i] = max(arr[i + 1] + 1, arr[i])

        return sum(arr)
```
---



**思路**
![](https://hackmd.io/_uploads/Sk4J-1qW6.png)


**講解連結**
https://www.youtube.com/watch?v=1IzCRCcK17A
Provided by. NeetCodeIO
