LeetCode 53最佳买卖时机

216 阅读1分钟

leetcode-cn.com/problems/be…

class Solution {
    fun maxProfit(prices: IntArray): Int {
        var max = 0
        for (i in 0 until prices.size) {
            println("i:" + i)
            var maxV2 = Int.MIN_VALUE
            var find = false
            for (j in i + 1 until prices.size) {
                if (prices[j] > maxV2) {
                    find = true
                    maxV2 = prices[j]
                }
            }
            println("cur:" + prices[i] + " maxV2:" + maxV2 +" cur max:"+max + " cha:"+(maxV2 - prices[i]))
            if (maxV2 - prices[i] > max && find) max = maxV2 - prices[i]
        }
        return max
    }
}