-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path120-Triangle.cs
31 lines (27 loc) · 940 Bytes
/
120-Triangle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//-----------------------------------------------------------------------------
// Runtime: 112ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _120_Triangle
{
public int MinimumTotal(IList<IList<int>> triangle)
{
if (triangle.Count == 0) return 0;
for (int i = 1; i < triangle.Count; i++)
for (int j = 0; j <= i; j++)
if (j == 0)
triangle[i][j] += triangle[i - 1][j];
else if (j == i)
triangle[i][j] += triangle[i - 1][j - 1];
else
triangle[i][j] += Math.Min(triangle[i - 1][j], triangle[i - 1][j - 1]);
return triangle.Last().Min();
}
}
}