Back to problems

Best Time to Buy and Sell Stock

Algorithm · Visa · Easy

Best Time to Buy and Sell Stock You are given an integer array prices where prices[i] is the price of a stock on the i-th day. You may complete at most one transaction: buy one share on a day of your choice and sell it on a later day (or do nothing). Design an algorithm that returns the maximum profit attainable. Example 1: Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6). Profit = 6 - 1 = 5. Note: You cannot sell…

Checking your access…