My two 36ms Codes - one pass O(1) space  plus another easy to understand code C++
class Solution { // O(n) single pass\n    public:\n        int candy(vector<int>& ratings) {\n            int ans = 0;\n            int i = 1; int dip = 1;\n   
    class Solution { // O(n) single pass\n    public:\n        int candy(vector<int>& ratings) {\n            int ans = 0;\n            int i = 1; int dip = 1;\n            if(ratings.size() == 1) return 1;\n            while(i < ratings.size())\n            {  \n                int len2 = 1;\n                while( i < ratings.size() && ratings[i] == ratings[i-1]){\n                    i++; ans += len2;\n                }\n                while(i < ratings.size() && ratings[i-1] < ratings[i]){\n                    len2++; i++;\n                } ans += len2*(len2+1)/2;\n                while( i < ratings.size() && ratings[i] == ratings[i-1]){\n                    i++; ans += 1;\n                    len2 = 1;\n                }\n                len2--;\n                int len = 0;\n                while( i < ratings.size() && ratings[i-1] > ratings[i]){\n                    len++;  i++;\n                }\n                if( i-1 != ratings.size()-1 ) { ans -= 1; }\n                ans += len*(len+1)/2;\n                if(len > len2) ans += len-len2;\n            }\n            return ans;\n        }\n    };\n\nEasy to understand 2 pass code:    \nIdea: Forward pass is intuitively ok and gives min candies, the second reverse pass doesn't break any thing from the previous pass and completes the reverse direction requirements too.\n\n    class Solution {\n    public:\n        int candy(vector<int>& arr) {\n            int cand[100001] = {0};\n            int n = arr.size();\n            for(int i = 1; i < n; i++) \n                if(arr[i] > arr[i-1]) cand[i] = cand[i-1] + 1;\n            for(int i = n-2; i>= 0; i--) \n                if(arr[i] > arr[i+1]) cand[i] = max(cand[i], cand[i+1] + 1);\n            int sum = n;\n            for(int i = 0; i < n; i++) sum += cand[i];\n            return sum;\n        }\n    };
['C++']