2 Approach || Brute Force ---> Better Approach || EASY EXPLANATION || C++ ||
\n# Approach\n#### 1st Approach\n- Go to every index and check how many neighbours are bigger than that max(left-neighbour, right-neighbour)\n\n#### Better Appr
\n# Approach\n#### 1st Approach\n- Go to every index and check how many neighbours are bigger than that ```max(left-neighbour, right-neighbour)```\n\n#### Better Approach\n- Make two vertors ```left and right```\n- ```left``` for storing the maximum neighbours to its recent left if any is equal to or bigger than the ```ith``` number then  we cannot further calculate\n- ```right``` for storing the maximum neighbours to its recent right\n- Now take the maximum of ```max(left[i], right[i] + 1``` and add it to the answer\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Approach 1:\n    - Time complexity -- $$O(n^2)$$\n    - Space complexity -- $$O(1)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Approach 2:\n    - Time complexity -- $$O(n)$$\n    - Space complexity -- $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n## Approach 1\n```\nclass Solution {\npublic:\n    int candy(vector<int>& ratings) {\n        int n = ratings.size();\n        if(n == 1)\n            return 1;\n\n        int ans = n; \n        int i = 0;\n        while(i < n){\n            int j = i - 1;\n            int count = 0;\n            while(j >= 0 && ratings[j + 1] > ratings[j]){\n                count++, j--;\n            }\n            int count2 = 0;\n            j = i + 1;\n            while(j < n && ratings[j - 1] > ratings[j]){\n                count2++, j++;\n            }\n            ans += max(count, count2);\n            i++;\n        }\n        return ans;\n    }\n};\n```\n\n## Approach 2\n```\nclass Solution {\npublic:\n    int candy(vector<int>& ratings) {\n        int n = ratings.size();\n        vector<int> left(n, 0), right(n, 0);\n        for(int i = 1; i < n; i++)\n            if(ratings[i] > ratings[i - 1])\n                left[i] = left[i - 1] + 1;\n\n        for(int i = n - 2; i >= 0; i--)\n            if(ratings[i] > ratings[i + 1])\n                right[i] = right[i + 1] + 1;\n\n        int ans = n;\n        for(int i = 0; i < n; i++)\n            ans += max(left[i], right[i]);\n        return ans;\n    }\n};\n\n```\n![upvote_leetcode.jpeg](https://assets.leetcode.com/users/images/4a49ab74-4aa1-4097-82a2-240682ea3352_1694580760.1023214.jpeg)\n\n
['C++']