My accepted solution is quite neat, but not sure if can further optimize it.
public class Solution {\n        public int candy(int[] ratings) {\n    \n            if(ratings.length < 2) return ratings.length;\n            int[] leftTrav 
    public class Solution {\n        public int candy(int[] ratings) {\n    \n            if(ratings.length < 2) return ratings.length;\n            int[] leftTrav = new int[ratings.length];\n            int[] rightTrav = new int[ratings.length];\n            for(int i=0; i<length; i++) {\n                leftTrav[i] = 1;\n                rightTrav[i] = 1;\n            }\n            \n            for(int i=0; i<length - 1; i++) {\n                int j = (length - 1) - i;\n                if(ratings[i+1] > ratings[i]) leftTrav[i+1] = leftTrav[i] + 1;\n                if(ratings[j-1] > ratings[j]) rightTrav[j-1] = rightTrav[j] + 1;\n            }\n            int total = 0;\n            for(int i=0; i<length; i++) total += Math.max(leftTrav[i], rightTrav[i]);\n            return total;\n        }\n    }
[]