def candy(self, ratings):
        """
        dp & re-adjustment

        algorithm: bit.ly/1sod4QC
        2 situations need to be considered
        1. fall below 1
        2. not fall sufficiently

        Wave crest
        Wave trough
        :param ratings: a list of int
        :return: int
        """
        length = len(ratings)
        dp = [-1 for _ in xrange(length)]
        dp[0] = 1
        for ind in xrange(1, length):
            val = ratings[ind]
            if ratings[ind-1]<val:
                dp[ind] = dp[ind-1]+1
            elif ratings[ind-1]>val:
                dp[ind] = dp[ind-1]-1
            else:
                dp[ind] = 1

            # trough in the middle
            if ind+1<length and ratings[ind-1]>val and val<=ratings[ind+1]:
                self.re_adjust(ratings, dp, ind)

        # trough at the end
        if ratings[length-2]>ratings[length-1]:
            self.re_adjust(ratings, dp, length-1)

        return sum(dp)
dataset/ETHPy150Open algorhythms/LeetCode/135 Candy.py/Solution.candy/original