< Summary

Information
Class: LeetCode.Algorithms.BestTimeToBuyAndSellStock5.BestTimeToBuyAndSellStock5DynamicProgramming
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\BestTimeToBuyAndSellStock5\BestTimeToBuyAndSellStock5DynamicProgramming.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 71
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaximumProfit(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\BestTimeToBuyAndSellStock5\BestTimeToBuyAndSellStock5DynamicProgramming.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.BestTimeToBuyAndSellStock5;
 13
 14/// <inheritdoc />
 15public 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)
 225    {
 226        var days = prices.Length;
 27
 228        var transactionStatesCount = maxTransactions + 1;
 29
 230        Span<long> profits = stackalloc long[transactionStatesCount];
 231        Span<long> longHoldProfits = stackalloc long[transactionStatesCount];
 232        Span<long> shortHoldProfits = stackalloc long[transactionStatesCount];
 33
 234        var initialLongProfit = -prices[0];
 235        var initialShortProfit = prices[0];
 36
 1437        for (var completedTransactions = 1; completedTransactions < transactionStatesCount; completedTransactions++)
 538        {
 539            longHoldProfits[completedTransactions] = initialLongProfit;
 540            shortHoldProfits[completedTransactions] = initialShortProfit;
 541        }
 42
 2843        for (var day = 1; day < days; day++)
 1244        {
 1245            long currentPrice = prices[day];
 46
 8847            for (var completedTransactions = maxTransactions; completedTransactions > 0; completedTransactions--)
 3248            {
 3249                var longHoldProfit = longHoldProfits[completedTransactions];
 3250                var shortHoldProfit = shortHoldProfits[completedTransactions];
 51
 3252                var profitAfterClosingLong = longHoldProfit + currentPrice;
 3253                var profitAfterClosingShort = shortHoldProfit - currentPrice;
 54
 3255                var profitAfterClosing = Math.Max(profitAfterClosingLong, profitAfterClosingShort);
 56
 3257                profits[completedTransactions] = Math.Max(profits[completedTransactions], profitAfterClosing);
 58
 3259                var previousTransaction = completedTransactions - 1;
 60
 3261                var openLongFromCash = profits[previousTransaction] - currentPrice;
 3262                var openShortFromCash = profits[previousTransaction] + currentPrice;
 63
 3264                longHoldProfits[completedTransactions] = Math.Max(longHoldProfit, openLongFromCash);
 3265                shortHoldProfits[completedTransactions] = Math.Max(shortHoldProfit, openShortFromCash);
 3266            }
 1267        }
 68
 269        return profits[^1];
 270    }
 71}