方法
方法1:左右扫描
- 从左向右移动:If ratings[i] > ratings[i-1], then candy[i] = candy[i-1]+1.
- 从右向左移动:If ratings[i] > ratings[i+1], then candy[i] = max(candy[i], candy[i+1]+1)
1 | class Solution(object): |
方法2:左右扫描 & 贪心算法
1 | class Solution(object): |
1 | class Solution(object): |
1 | class Solution(object): |