-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution673.cs
54 lines (47 loc) · 1.36 KB
/
Solution673.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution673
{
/// <summary>
/// 673. Number of Longest Increasing Subsequence - Medium
/// <a href="https://leetcode.com/problems/number-of-longest-increasing-subsequence">See the problem</a>
/// </summary>
public int FindNumberOfLIS(int[] nums)
{
var n = nums.Length;
var lengths = new int[n];
var counts = new int[n];
var maxLength = 0;
var result = 0;
for (var i = 0; i < n; i++)
{
lengths[i] = 1;
counts[i] = 1;
for (var j = 0; j < i; j++)
{
if (nums[i] > nums[j])
{
if (lengths[j] + 1 > lengths[i])
{
lengths[i] = lengths[j] + 1;
counts[i] = counts[j];
}
else if (lengths[j] + 1 == lengths[i])
{
counts[i] += counts[j];
}
}
}
if (lengths[i] > maxLength)
{
maxLength = lengths[i];
result = counts[i];
}
else if (lengths[i] == maxLength)
{
result += counts[i];
}
}
return result;
}
}