-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpLongestIncreasingSubsequenceProblemInstance.cs
63 lines (59 loc) · 2.02 KB
/
dpLongestIncreasingSubsequenceProblemInstance.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
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class dpLongestIncreasingSubsequenceProblemInstance
{
static int maxSum = 0;
static int currentSum = 0;
public static void Main1()
{
int[] nums = new int[] { 1, 101, 2, 3, 100, 4, 5 };
List<int> longestSubsequence = new List<int>();
longestSubsequence.Add(nums[0]);
currentSum = nums[0];
maxSum = nums[0];
for (int i = 1; i < nums.Length; i++)
{
BinarySearchThenAddIncreasing(longestSubsequence, nums[i], 0, longestSubsequence.Count);
}
Console.WriteLine(maxSum);
Console.ReadLine();
}
public static void BinarySearchThenAddIncreasing(List<Int32> list, int number,int l , int r)
{
if(number < list[0])
{
list[0] = number;
}
else if (number > list[list.Count -1])
{
list.Add(number);
currentSum += number;
maxSum = Math.Max(maxSum, currentSum);
}
else
{
int middle = (l + r) / 2;
if (number > list[middle] && number <= list[middle + 1])
{
currentSum = currentSum - list[middle + 1];
list[middle + 1] = number;
currentSum = currentSum + number;
maxSum = Math.Max(maxSum, currentSum);
}
else if(number <= list[middle])
{
BinarySearchThenAddIncreasing(list, number, l, middle);
}
else
{
BinarySearchThenAddIncreasing(list, number, middle + 1, r);
}
}
}
}
}