ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
StockTrading.h

The largest gain of buying and selling a stock with at most \( k \), or with exactly \( k \), transactions, given its prices on \( n \) days. More...

Files

file  ADS/optimization/StockTrading.h
 The largest gain of buying and selling a stock with at most \( k \), or with exactly \( k \), transactions, given its prices on \( n \) days: a transaction buys on a day \( b \) and sells on a later day \( s \), and the transactions do not overlap, \( b_1 < s_1 < b_2 < s_2 < \cdots < b_k < s_k \).

Functions

template<class T>
T ADS::maxProfit (const std::vector< T > &prices, std::size_t k)
 the largest gain of at most \( k \) transactions on the prices
template<class T>
std::optional< T > ADS::maxProfitExactly (const std::vector< T > &prices, std::size_t k)
 the largest gain of exactly \( k \) transactions on the prices, nothing if there are fewer than \( 2 k \) days

Detailed description

The largest gain of buying and selling a stock with at most \( k \), or with exactly \( k \), transactions, given its prices on \( n \) days.

The definitions, the invariants and the costs of the area are in Theory of optimization.

Function documentation

◆ maxProfit()

template<class T>
T ADS::maxProfit ( const std::vector< T > & prices,
std::size_t k )

the largest gain of at most \( k \) transactions on the prices

In pseudocode (beyond the book, the scan of the exercise B3 of 12/01/2026, Borsa, of its Esercizi Programmazione dinamica), \( \mathit{free} \) and \( \mathit{hold} \) starting at \( \textbf{nil} \) but \( \mathit{free}[ 0 ] = 0 \), a \( \textbf{nil} \) losing every maximum:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxProfit}( x[ 0 .. n - 1 ] , k ) \cr 2 & \quad \textbf{if}\ k \ge n / 2\ \textbf{then return}\ \sum_{i \ge 1} \max\{ x[ i ] - x[ i - 1 ] , 0 \} \cr 3 & \quad \textbf{foreach}\ \text{price } y \text{ of } x\ \textbf{do} \cr 4 & \qquad \textbf{for}\ l \gets k\ \textbf{downto}\ 1\ \textbf{do} \cr 5 & \qquad\quad \mathit{free}[ l ] \gets \max\{ \mathit{free}[ l ] , \mathit{hold}[ l ] + y \} \cr 6 & \qquad\quad \mathit{hold}[ l ] \gets \max\{ \mathit{hold}[ l ] , \mathit{free}[ l - 1 ] - y \} \cr 7 & \quad \textbf{return}\ \max_l \mathit{free}[ l ] \end{array}} \]

Postcondition
the result is not negative

◆ maxProfitExactly()

template<class T>
std::optional< T > ADS::maxProfitExactly ( const std::vector< T > & prices,
std::size_t k )

the largest gain of exactly \( k \) transactions on the prices, nothing if there are fewer than \( 2 k \) days

In pseudocode (exercise B3 of 12/01/2026, Borsa, of the Esercizi Programmazione dinamica of the book), the scan of maxProfit() with no shortcut:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxProfitExactly}( x[ 0 .. n - 1 ] , k ) \cr 2 & \quad \textbf{if}\ n < 2 k\ \textbf{then return nil} \cr 3 & \quad \text{run lines 3 to 6 of } \mathrm{maxProfit} \cr 4 & \quad \textbf{return}\ \mathit{free}[ k ] \end{array}} \]