| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.BestTimeToBuyAndSellStock5; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class BestTimeToBuyAndSellStock5DynamicProgramming : IBestTimeToBuyAndSellStock5 |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n * k) |
| | | 19 | | /// Space complexity - O(k) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="prices"></param> |
| | | 22 | | /// <param name="maxTransactions"></param> |
| | | 23 | | /// <returns></returns> |
| | | 24 | | public long MaximumProfit(int[] prices, int maxTransactions) |
| | 2 | 25 | | { |
| | 2 | 26 | | var days = prices.Length; |
| | | 27 | | |
| | 2 | 28 | | var transactionStatesCount = maxTransactions + 1; |
| | | 29 | | |
| | 2 | 30 | | Span<long> profits = stackalloc long[transactionStatesCount]; |
| | 2 | 31 | | Span<long> longHoldProfits = stackalloc long[transactionStatesCount]; |
| | 2 | 32 | | Span<long> shortHoldProfits = stackalloc long[transactionStatesCount]; |
| | | 33 | | |
| | 2 | 34 | | var initialLongProfit = -prices[0]; |
| | 2 | 35 | | var initialShortProfit = prices[0]; |
| | | 36 | | |
| | 14 | 37 | | for (var completedTransactions = 1; completedTransactions < transactionStatesCount; completedTransactions++) |
| | 5 | 38 | | { |
| | 5 | 39 | | longHoldProfits[completedTransactions] = initialLongProfit; |
| | 5 | 40 | | shortHoldProfits[completedTransactions] = initialShortProfit; |
| | 5 | 41 | | } |
| | | 42 | | |
| | 28 | 43 | | for (var day = 1; day < days; day++) |
| | 12 | 44 | | { |
| | 12 | 45 | | long currentPrice = prices[day]; |
| | | 46 | | |
| | 88 | 47 | | for (var completedTransactions = maxTransactions; completedTransactions > 0; completedTransactions--) |
| | 32 | 48 | | { |
| | 32 | 49 | | var longHoldProfit = longHoldProfits[completedTransactions]; |
| | 32 | 50 | | var shortHoldProfit = shortHoldProfits[completedTransactions]; |
| | | 51 | | |
| | 32 | 52 | | var profitAfterClosingLong = longHoldProfit + currentPrice; |
| | 32 | 53 | | var profitAfterClosingShort = shortHoldProfit - currentPrice; |
| | | 54 | | |
| | 32 | 55 | | var profitAfterClosing = Math.Max(profitAfterClosingLong, profitAfterClosingShort); |
| | | 56 | | |
| | 32 | 57 | | profits[completedTransactions] = Math.Max(profits[completedTransactions], profitAfterClosing); |
| | | 58 | | |
| | 32 | 59 | | var previousTransaction = completedTransactions - 1; |
| | | 60 | | |
| | 32 | 61 | | var openLongFromCash = profits[previousTransaction] - currentPrice; |
| | 32 | 62 | | var openShortFromCash = profits[previousTransaction] + currentPrice; |
| | | 63 | | |
| | 32 | 64 | | longHoldProfits[completedTransactions] = Math.Max(longHoldProfit, openLongFromCash); |
| | 32 | 65 | | shortHoldProfits[completedTransactions] = Math.Max(shortHoldProfit, openShortFromCash); |
| | 32 | 66 | | } |
| | 12 | 67 | | } |
| | | 68 | | |
| | 2 | 69 | | return profits[^1]; |
| | 2 | 70 | | } |
| | | 71 | | } |