4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

One algorithm a day. Write a function that returns best profit you could made from trading.

Write an efficient function that takes stockPrices and returns the best profit I could have made from one purchase and one sale of one share of Apple stock yesterday.

func getMaxProfit(_ prices:[Double])->Double{
    //we need to track a difference
    guard prices.count > 0 else{
        return 0
    }
    
    var priceDifference = 0.0
    
    for time in 0..<prices.count{
        let price = prices[time]
        for laterTime in time + 1..<prices.count{
            let laterPrice = prices[laterTime]
            if (laterPrice - price) > priceDifference{
                priceDifference = (laterPrice - price)
            }
        }
    }
    return priceDifference
}

let stockPrices:[Double] = [10, 7, 5, 8, 11, 9]
getMaxProfit(stockPrices)
2 upvotes $0.00

Replies (0)

Conversation

No replies yet

Replies will appear here as people join the conversation.

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: