Press "Enter" to skip to content

Posts tagged as “max subarray”

花花酱 LeetCode 121. Best Time to Buy and Sell Stock

题目大意: 给你一只股票每天的价格,如果只能做一次交易(一次买进一次卖出)问你最多能赚多少钱。


Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Example 1:

Example 2:




Solution 1:


C++ / reduce to maximum subarray

Related Problems: