Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
439 B
8
Indexable
Never
def giveCandy2(ratings):
    n = len(ratings)

    increasing = [1]

    for i in range(1, n):
        increasing.append(increasing[i-1]+1 if ratings[i] > ratings[i-1] else 1)

    decreasing = [1]
    for i in range(n-2, -1, -1):
        decreasing.append(decreasing[-1]+1 if ratings[i] > ratings[i+1] else 1)

    total = 0

    for i in range(n):
        total += max(increasing[i], decreasing[n-1-i])

    return total