My very first solution contribution
Intuition\n Describe your first thoughts on how to solve this problem. \nMy very first solution contribution, based on my Python solution\n# Approach\n Describe
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy very first solution contribution, based on my Python solution\n# Approach\n<!-- Describe your approach to solving the problem. -->\nInstead of using loop twice, to solve 3 cases happened between ratings[i] and ratings[i-1]:\n- ratings[i]==ratings[i-1]: new start, because no condition\n- ratings[i]>ratings[i-1]: increase, and record the maximum\n- ratings[i]<ratings[i-1]: add 1 at new position, but be careful for the violation:\n    a. The candies from previous maximum index to the i-1th should increase by 1 to maintain the relation\n    b. The candies from 1+previous maximum index should increase by 1.\nI created 3 variables for curr candy (val), index of previous \'peak\' (maxIdx) and number of candies at the previous peak (maxVal)\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\npublic class Solution {\n    public int Candy(int[] ratings) {\n        int maxVal = 1;\n        int val = 1;\n        int ans = 1;\n        int maxIdx = 0;\n        for(int i = 1; i<ratings.Length;i++){\n            if(ratings[i]> ratings[i-1]){\n                ++val;\n                ans += val;\n                maxIdx = i;\n                maxVal = val;\n            }\n            else if(ratings[i] < ratings[i-1]) {\n                val = 1;\n                ans+= i - maxIdx;\n                if (i - maxIdx == maxVal) {\n                    maxVal+=1;\n                    ans+=1;\n                }\n            }\n            else {\n                maxVal = 1;\n                val =1;\n                maxIdx = i;\n                ans +=1;\n            }\n        }\n        return ans;\n\n\n        \n    }\n}\n```
['C#']