< Summary

Information
Class: LeetCode.Algorithms.MinimumFallingPathSum2.MinimumFallingPathSum2DynamicProgramming
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumFallingPathSum2\MinimumFallingPathSum2DynamicProgramming.cs
Line coverage
100%
Covered lines: 41
Uncovered lines: 0
Coverable lines: 41
Total lines: 78
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinFallingPathSum(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumFallingPathSum2\MinimumFallingPathSum2DynamicProgramming.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 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.MinimumFallingPathSum2;
 13
 14/// <inheritdoc />
 15public class MinimumFallingPathSum2DynamicProgramming : IMinimumFallingPathSum2
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="grid"></param>
 22    /// <returns></returns>
 23    public int MinFallingPathSum(int[][] grid)
 224    {
 225        var n = grid.Length;
 26
 227        if (n == 1)
 128        {
 129            return grid[0][0];
 30        }
 31
 132        var dp = new int[n];
 33
 134        Array.Copy(grid[0], dp, n);
 35
 636        for (var i = 1; i < n; i++)
 237        {
 238            var prev = new int[n];
 39
 240            Array.Copy(dp, prev, n);
 41
 242            var newDp = new int[n];
 43
 444            int min1 = int.MaxValue, min2 = int.MaxValue;
 245            var idx1 = -1;
 46
 1647            for (var j = 0; j < n; j++)
 648            {
 649                if (prev[j] < min1)
 250                {
 251                    min2 = min1;
 252                    min1 = prev[j];
 253                    idx1 = j;
 254                }
 455                else if (prev[j] < min2)
 256                {
 257                    min2 = prev[j];
 258                }
 659            }
 60
 1661            for (var j = 0; j < n; j++)
 662            {
 663                if (j == idx1)
 264                {
 265                    newDp[j] = grid[i][j] + min2;
 266                }
 67                else
 468                {
 469                    newDp[j] = grid[i][j] + min1;
 470                }
 671            }
 72
 273            dp = newDp;
 274        }
 75
 176        return dp.Prepend(int.MaxValue).Min();
 277    }
 78}