Advertisement
DeepRest

Candy

Jul 4th, 2022
926
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data Copied
Advertisement