best-time-to-buy-and-sell-stock 1.0.0
Best Time to Buy and Sell Stock
Loading...
Searching...
No Matches
Solution Class Reference

Public Member Functions

int maxProfit (vector< int > &prices)
 

Detailed Description

Definition at line 6 of file main.cpp.

Member Function Documentation

◆ maxProfit()

int Solution::maxProfit ( vector< int > &  prices)
inline

Definition at line 9 of file main.cpp.

10 {
11 int length = (int)prices.size();
12 if (length <= 1)
13 return 0;
14 int maximal_profit = 0;
15 int buy_price = prices[0];
16 for (int i = 1; i < length; ++i)
17 {
18 if (prices[i] < buy_price)
19 buy_price = prices[i];
20 else if (prices[i] > buy_price)
21 maximal_profit = max(maximal_profit, prices[i]);
22 }
23 return maximal_profit;
24 }

Referenced by main().


The documentation for this class was generated from the following file: