| | 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 | |
|
| | 12 | | namespace LeetCode.Algorithms.PascalsTriangle; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class PascalsTriangleDynamicProgramming : IPascalsTriangle |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n^2) |
| | 19 | | /// Space complexity - O(n^2) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="numRows"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public IList<IList<int>> Generate(int numRows) |
| 2 | 24 | | { |
| 2 | 25 | | var result = new List<IList<int>> { new List<int> { 1 } }; |
| | 26 | |
|
| 12 | 27 | | for (var i = 1; i < numRows; i++) |
| 4 | 28 | | { |
| 4 | 29 | | var row = new List<int> { 1 }; |
| | 30 | |
|
| 20 | 31 | | for (var j = 1; j < i; j++) |
| 6 | 32 | | { |
| 6 | 33 | | var value = result[i - 1][j - 1] + result[i - 1][j]; |
| | 34 | |
|
| 6 | 35 | | row.Add(value); |
| 6 | 36 | | } |
| | 37 | |
|
| 4 | 38 | | row.Add(1); |
| 4 | 39 | | result.Add(row); |
| 4 | 40 | | } |
| | 41 | |
|
| 2 | 42 | | return result; |
| 2 | 43 | | } |
| | 44 | | } |