100% Easy One Pass Solution with explanation and example visualization
\n# Approach\nFirstly we\'ll create an array left of size n+1, this array will store the number of candies to be allotted to ith child considering his neighbour
\n# Approach\nFirstly we\'ll create an array ```left``` of ```size n+1```, this array will store the ```number of candies``` to be allotted to ith child considering his ```neighbours to left```.\n\nTraverse through the ratings array and if rating of current child is more than rating of previous child, increase the number of candies by 1, i.e ```left[i] = left[i-1] + 1```.\n\nWe\'re accounting for the left neighbours, now we need to account for right neigbours, for this we can have another for loop which will run from ```right to left```, but this will just ```add on to space complexity```.\n\nOne thing that we notice in our left for loop is that we need to increment only when the current child\'s rating is greater than previous child\'r rating.\n\nSo in the for loop for ```right neighbour```, we will check if rating of current child is greater than one to its right, if it is we will increment the value of right and do ```left[i] = max(left[i], right)```.\n\nBy doing this we are incrementing the value of right ```only when the current rating is greater than next```, if its not our ```right``` stands at ```1```, this way we don\'t need to store our candies for right neighbours in another array and then later take maximum, so we ```save ourselves a lot of time and space```.\n\nAt last find the ```sum``` of candies and ```return sum```.\n\n# I\'ve explained an example below for better understanding-\n![image.png](https://assets.leetcode.com/users/images/73408bf6-d2b5-4c93-ab81-a532d5210989_1694580577.7185543.png)\n\n\n# Do upvote if you liked even a bit!\n\n# C++ Code\n```\nclass Solution {\npublic:\n    int candy(vector<int>& ratings) {\n        int n=ratings.size();\n      vector<int> left(n+1,1);\n      for(int i=1;i<n;i++){\n          if(ratings[i]>ratings[i-1]) left[i]=left[i-1]+1;\n      }  \n\n      int right=1;\n      for(int i=n-2;i>=0;i--){\n          if(ratings[i]>ratings[i+1]){\n            right++;\n            left[i]=max(left[i],right);\n          }else right=1;\n      }\n\n      int sum=0;\n      for(int i=0;i<n;i++){\n            sum+=left[i];\n      }\n      return sum;\n    }\n};\n```\n# JAVA Code\n```\npublic class Solution {\n    public int candy(int[] ratings) {\n        if (ratings == null || ratings.length == 0) return 0;\n        int total = 1, prev = 1, countDown = 0;\n        for (int i = 1; i < ratings.length; i++) {\n            if (ratings[i] >= ratings[i-1]) {\n                if (countDown > 0) {\n                    total += countDown*(countDown+1)/2; // arithmetic progression\n                    if (countDown >= prev) total += countDown - prev + 1;\n                    countDown = 0;\n                    prev = 1;\n                }\n                prev = ratings[i] == ratings[i-1] ? 1 : prev+1;\n                total += prev;\n            } else countDown++;\n        }\n        if (countDown > 0) { // if we were descending at the end\n            total += countDown*(countDown+1)/2;\n            if (countDown >= prev) total += countDown - prev + 1;\n        }\n        return total;\n    }\n}\n```
['C++', 'Java']