C++ easy to understand? Solution with lot of comments ( O(n), constant space, one pass)
Let me know if you find it easy please.\n\n    Ratings:\n                        Peak\n            Peak        |\n            |           | |\n          | |    
Let me know if you find it easy please.\n\n    Ratings:\n                        Peak\n            Peak        |\n            |           | |\n          | |           | | |   \n        | | | |       | | | | |       | | |\n      | | | | | |   | | | | | | |     | | |\n    | | | | | | | | | | | | | | | | | | | |\n                    Candies:\n    1 2 3 4 5 3 2 1 2 3 5 4 3 2 1 1 1 2 1 1\n    x---a---x\n            x--b--x    \n\n\n\n\n\n    \n\n\n\n It seems easy to track the length of the slopes of the mountains (sequence of all ascending or \n all descending ratings)\n and then sum the candies for the two slopes (an easy n*(n+1)/2), with land sequences adding +1 each \n rating value. \n\n It almost works.\n Big problem is we don't know who the peak belongs (left or right slope?)\n\n Example: the first sequence "a" is made of 5 ascending elements. \n    The second sequence "b" is made of 4 descending elements.\n    Which slope gets the peak? \n\n Answer: the longest. So for the first mountain "a" slope is\n \n    L(a)=4    ==>  4+3+2+1 candies \n\nand "b" is \n\n    L(b)=3 ==> 3+2+1 candies\n\nPeak is \n\n    max(L(a),L(b)) + 1 ==>5 candies.\n         \n\n Unfortunately we can know the longer side of the mountain at the end of the mountain only.\n\n So we use "up" and "down" variable to track the length of the slopes. We sum the candies \n at the end of each mountain and then we reset the slopes to zero.\n\n Special case: we have same rating twice (land). This ends a mountain even if it was ascending \n (that means: descending slope of length = 0)\n\n And to make things more complicated, the last element of a sequence \n is also the beginning of the next sequence (another mountain or land),\n so we take that into account.\n\n\n    class Solution {\n    public:\n        // Candies for a slope of n elements\n        int slope(int n)\n        {\n            return (n*(n+1))/2;\n        }\n    \n        int candy(vector<int> &ratings) {\n            if (ratings.size() <= 1 )\n            {\n                return ratings.size();\n            }\n            int tot = 0;        // The final amount of candies\n            int up = 0;         // Lenght of the last ascending slope\n            int down = 0;       // Lenght of the last descending slope\n            int oldsign = 0;    // Previous ratings trend (1 = ascending, -1 == descending, 0 = stable) \n    \n            for (int i = 1; i< ratings.size(); i++)\n            {\n                            // Current ratings trend, same meaning as oldsign\n                int newsign = ratings[i] > ratings[i-1] ? 1 : \n                              ratings[i] < ratings[i-1] ? -1 : 0;\n                            // We can calculate the contribution of a slope \n                            // when that slope ends and it's not the peak of a mountain,\n                            // that's why we have "newsign == 0" and not "newsign <= 0"\n                if ((oldsign > 0 && newsign == 0)  ||\n                    (oldsign < 0  && newsign >= 0) )\n                    {\n                            // The peak of the mountain should have max(up,down)+1\n                            // candies, but the end of the mountain is the beginning of\n                            // the next sequence, so we don't the candy at the end of \n                            // the mountain\n                        tot += slope(up) + slope(down) + std::max(up, down);\n                        up = 0;\n                        down = 0;\n                    }\n    \n                if(newsign > 0)\n                    up++;\n                if(newsign < 0)\n                    down++;\n                        // Same rating of the previous child?\n                        // Take one candy.If you are better then the next child\n                        // the descending slope will take that into account, don't worry\n                if(newsign == 0)\n                    tot++;\n                    \n                oldsign = newsign;\n            }\n            \n                        // Final sequence has to be closed\n                        // Why +1? Because we don't have a sequence next!\n            tot += ramp(up) + ramp(down) + std::max(up, down) + 1;\n            return tot;\n        }\n    };
[]