def maxProfit(self, k, prices):
        n = len(prices)
        if k >= n:
            return self.maxProfit_unlimited_transactions(prices)

        l = [0 for _ in xrange(k+1)]  # local max
        g = [0 for _ in xrange(k+1)]  # global max
        gmax = 0
        for i in xrange(1, n):
            diff = prices[i] - prices[i-1]
            for j in xrange(k, 0, -1):
                l[j] = max(g[j-1]+diff, l[j]+diff)
                g[j] = max(l[j], g[j])
                gmax = max(gmax, g[j])

        return gmax
DP
dataset/ETHPy150Open algorhythms/LeetCode/188 Best Time to Buy and Sell Stock IV.py/Solution.maxProfit