Candy | Java Solution | Simple Approach
Approach: \n\n\n\n\nclass Solution {\n    public int candy(int[] ratings) {\n        int n =  ratings.length;\n        if(n==0)\n            return 0;\n        
**Approach: **\n![image](https://assets.leetcode.com/users/images/8e625675-840e-438f-9733-90d8a5f60a05_1656919335.6807528.jpeg)\n![image](https://assets.leetcode.com/users/images/ad4ee21e-ec47-434d-81d0-b534cf240158_1656919335.635522.jpeg)\n\n```\nclass Solution {\n    public int candy(int[] ratings) {\n        int n =  ratings.length;\n        if(n==0)\n            return 0;\n        int left[] = new int[n];\n        Arrays.fill(left,1);\n        for(int i=1;i<n;i++)\n        {\n            if(ratings[i] > ratings[i-1])\n                left[i] = left[i-1]+1;\n        }\n        int right[] = new int[n];\n        Arrays.fill(right,1);\n        for(int i=n-2;i>=0;i--)\n        {\n            if(ratings[i] > ratings[i+1])\n                right[i] = right[i+1]+1;\n        }\n        int ans = 0;\n        for(int i=0;i<n;i++)\n            ans += Math.max(left[i],right[i]);\n        return ans;\n    }\n}\n```\n\nTime Complexity : O(n)\nSpace Complexity : O(n) coz we are taking two arrays here\n\n**Though this is the accepted solution here, this can be optimised more:\nThe optimal solution is here:\n[https://leetcode.com/problems/candy/discuss/2235735/Candy-or-Java-Solution-or-Optimized](http://)**
['Java']