Intuitive O(N) time and O(1) explained like a 5th grader
When I read this question the thing that came into my mind was that which person will get 1 candy. So I imagined ratings as heights of towers and then realized 
When I read this question the thing that came into my mind was that which person will get 1 candy. So I imagined ratings as heights of towers and then realized that locally minimum tower will get 1 candy.\n\n- Consider ratings as heights of towers as shown in the figure below.\n- First of all we build infinite size towers to left and right of the array.\n- We define pits as the points, which have tower size as local minimum,\n  i.e. height of tower just to the left >= height_pit <= height of tower just to the right\n- In the below figure P1,P2...P5 are pits\n\n```\nRatings:\n\nInf                                      Inf\n|                   Peak                  |\n|       Peak          |                   |\n|         |           | |                 |\n|       | |           | | |               |\n|     | | | |       | | | | |     | | |   |\n|   | | | | | |   | | | | | | |   | | |   |\n| _ | | | | | | | | | | | | | | _ | | | | |\n-------------------------------------------\n* 0 2 3 4 5 3 2 1 2 3 6 5 4 3 2 0 3 3 3 1 *\ni=0 1 2 3 4 5 6 7 8 9...             ...19\n  ^\t\t        ^               ^   ^   ^\n  P1\t\t    P2              P3  P4  P5\n```\n\n- We assign candy=1 (minimum candy) to the pit tower and go left and right to the pit tower till we encounter peak. Assigning candy to the towers by increasing it by 1.\n- For example if we start with pit P1, then we assign\n\t- candy=1 to tower at i=0\n\t- candy=2 to tower at i=1\n\t- candy=3 to tower at i=2\n\t- candy=4 to tower at i=3\n\t- candy=5 to tower at i=4\n\t- **we have now reached the peak so we store the peakIndex and peakVal**\n\t- we store peakIndex and peakVal only when we go to the right of pit\n\n- When we go to the left of the pit and if we reach the peak which already has candy assigned so we check if the new candy we are carrying is greater than the candy already assigned, if yes then we remove the original candy assigned and assign it the new candy.\n- For example, when we start with pit=P2 and keep going left we see that when we reach peak at i=4\n\t- new candy = 4 \n\t- But old candy assigned = 5, so we don\'t assign it new candy\n\t\nHence we start with \n```\npeakIndex = None  #index of peak to the right of the pit\npeakVal = None  # candy assigned to the peak\ntotalSum = 0 # total candies assigned\n```\n\n**Python code:**\n\n```\ndef candy(self, L):\n    #whenever you encounter a pit then assign values to towers by going left and right\n    \n    #creating infinte size towers to left and right borders\n    L.insert(0,float(\'inf\'))\n    L.append(float(\'inf\'))\n    \n    peakIndex = None\n    peakVal = None\n    totalCandy = 0\n    \n    for i in xrange(1,len(L)-1):\n        if L[i-1]>=L[i]<=L[i+1]:\n            #means this point is a pit so start filling left and right\n            \n            # +1 corresponds to assigning candy=1 to the pit\n            totalCandy+=1\n            \n            #going left\n            index = i-1\n            curr_candy = 2\n            \n            #index>=1 so that we don\'t reach the left infinite tower\n            while index>=1 and L[index]>L[index+1]:\n                #if we encounter peak\n                if index==peakIndex:\n                    totalCandy -= peakVal\n                    totalCandy += max(peakVal,curr_candy)\n                else:\n                    totalCandy += curr_candy\n                index-=1\n                curr_candy+=1\n                \n            #going right\n            index = i+1\n            curr_candy = 2\n            \n            #index<len(L)-1 so that we don\'t reach the right infinite tower\n            while index<len(L)-1 and L[index]>L[index-1]:\n                totalCandy += curr_candy\n                index+=1\n                curr_candy+=1\n            \n            #updating encountered peak\n            peakIndex = index-1\n            peakVal = curr_candy-1\n    return totalCandy\n```
[]