Advertisement
DeepRest

Candy

Jul 4th, 2022
1,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. class Solution:
  2.     def candy(self, ratings: List[int]) -> int:
  3.         n = len(ratings)
  4.         standings = [(rate, index) for index, rate in enumerate(ratings)]
  5.         standings.sort()
  6.         candies = [1]*n
  7.        
  8.         for rate, person in standings:  
  9.             if person-1 >= 0 and ratings[person-1] < ratings[person]:
  10.                 candies[person] = max(candies[person], candies[person-1]+1)
  11.             if person+1 < n and ratings[person+1] < ratings[person]:
  12.                 candies[person] = max(candies[person], candies[person+1]+1)  
  13.        
  14.         return sum(candies)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement