Skip to content

Latest commit

 

History

History
36 lines (28 loc) · 895 Bytes

File metadata and controls

36 lines (28 loc) · 895 Bytes

LeetCode Problems

121. Best Time to Buy and Sell Stock

import kotlin.math.max
class Solution {
    fun maxProfit(prices: IntArray): Int {
        /*
            Analysis.
            track from last to start.
            calculate best profit by checking max-selling-point and max-buying-point.
         */
        var maxi = prices.last()
        var maxProfit = 0
        // iterate
        for (i in prices.size - 2 downTo 0) {
            // record best selling point
            maxi = max(maxi, prices[i])
            // record best real-profit ( best selling - cheapest buying )
            maxProfit = max(maxProfit, maxi - prices[i])
        }
        return maxProfit
    }
}