Untitled
unknown
swift
5 years ago
450 B
10
Indexable
import Foundation
func profit(prices: [Int]) -> Int {
var currentMaximum = 0
var totalProfit = 0
for price in prices.reversed() {
if price > currentMaximum {
currentMaximum = price
} else {
totalProfit += currentMaximum - price
}
}
return totalProfit
}
print(profit(prices: [6, 5, 4, 3, 2, 1]))
print(profit(prices: [1, 6, 5, 10, 8, 7]))
print(profit(prices: [1, 2, 10, 2, 4, 6]))Editor is loading...