Very Simple Java Solution with detail explanation
We take ratings array as [5, 6, 2, 2, 4, 8, 9, 5, 4, 0, 5, 1]\n\nIn the given problem each student will have at least 1 candy. So distribute 1 candy to each.\n\
    We take ratings array as [5, 6, 2, 2, 4, 8, 9, 5, 4, 0, 5, 1]\n\nIn the given problem each student will have at least 1 candy. So distribute 1 candy to each.\n\n    ratings:     [5, 6, 2, 2, 4, 8, 9, 5, 4, 0, 5, 1]\n    candies:     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n\nNow traverse the array from left to right. If the rating of (n+1) child is greater than (n) child then set the candy of (n+1) child as one candy more than the (n) child candies.\n\n    ratings:     [5, 6, 2, 2, 4, 8, 9, 5, 4, 0, 5, 1]\n    candies:     [1, 2, 1, 1, 2, 3, 4, 1, 1, 1, 2, 1]\n\nNow  traverse the array from right to left. If the (n) child rating is more than (n+1) child and (n) child candies is less than one more than (n+1) child candies then update the candies of (n) child as 1+ (n+1) candies.\n\n    ratings:     [5, 6, 2, 2, 4, 8, 9, 5, 4, 0, 5, 1]\n    candies:     [1, 2, 1, 1, 2, 3, 4, 3, 2, 1, 2, 1]\n\nTotal minimum candies: 23\n\n    public int candy(int[] ratings) {\n            int sum=0;\n            int[] a=new int[ratings.length];\n            for(int i=0;i<a.length;i++)\n            {\n                a[i]=1;\n            }\n            for(int i=0;i<ratings.length-1;i++)\n            {\n                if(ratings[i+1]>ratings[i])\n                {\n                    a[i+1]=a[i]+1;\n                }\n            }\n            for(int i=ratings.length-1;i>0;i--)\n            {\n                if(ratings[i-1]>ratings[i])\n                {\n                    if(a[i-1]<(a[i]+1))\n                    {\n                        a[i-1]=a[i]+1;\n                    }\n                }\n            }\n            for(int i=0;i<a.length;i++)\n            {\n                sum+=a[i];\n            }\n            return sum;\n        }
['Greedy', 'Java']