from typing import List

class Solution:
    def candy(self, ratings: List[int]) -> int:
        if not ratings:
            return 0
        
        size = len(ratings)
        new_current = [1] * size
        cnt = 1  # 糖果总数
        current = 1  # 当前连续增加或减少的糖果数
        old = ratings[0]  # 前一个评分
        
        for i in range(1, size):
            k = 0
            if old > ratings[i]:  # 如果当前评分低于前一个评分
                current = 1  # 重置当前糖果数为 1
                # for j in range(0,i):
                #     if new_current[j] == 1:
                #         k += 1
                cnt = cnt + i  # 减去之前的索引
            elif old < ratings[i]:  # 如果当前评分高于前一个评分
                current += 1  # 当前糖果数加 1
            else:  # 如果当前评分与前一个评分相等
                current -= 1  # 当前糖果数减 1
            new_current[i] = current
            cnt = cnt +  current  # 更新糖果总数
            old = ratings[i]  # 更新前一个评分
            
        return cnt

if __name__ == '__main__':
    solution = Solution()
    print(solution.candy([1,3,2,2,1]))