-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJobSequencingDeadline.cs
52 lines (47 loc) · 2.29 KB
/
JobSequencingDeadline.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DynamicProgrammingAlgorithms
{
class JobSequencingDeadline
{
//List<KeyValuePair<DateTime, double>> DateTime stands for deadline and double for profit
//Accepting all the jobs have 1 unit of work to finish
//key corresponds to deadline and value to profit
public KeyValuePair<KeyValuePair<string, string>, double>[] JobSequencingWithDeadlines(
List<KeyValuePair<DateTime, double>> jobListWithDeadlines)
{
//Jobs and their profit enlisted in descending order , on profit base
List<KeyValuePair<DateTime, double>> tempList = jobListWithDeadlines.OrderByDescending(x => x.Value).ToList();
//Distinct datetime values are filtered out
List<DateTime> deadLineList = jobListWithDeadlines.Select(x => x.Key.Date).Distinct().ToList();
deadLineList.Insert(0, DateTime.UtcNow.Date);// today's date is added
deadLineList.Sort(); // Datetime list sorted, values will be inserted according to this order
int count = 0;
int sizeOfArray = deadLineList.Count - 1;
int repeat = tempList.Count() - 1;
//Array for keeping high profit values
KeyValuePair<KeyValuePair<string, string>, double>[] deadLineSequence =
new KeyValuePair<KeyValuePair<string, string>, double>[sizeOfArray];
for (int i = 0; i < repeat; i++)
{
for (int k = deadLineList.IndexOf(tempList[i].Key.Date); k > 0; k--)
{
if (deadLineSequence[k - 1].Key.Value == null)
{
deadLineSequence[k - 1] = new KeyValuePair<KeyValuePair<string, string>, double>(
new KeyValuePair<string, string>(deadLineList[k - 1].ToString("d")
, deadLineList[k].ToString("d")), tempList[i].Value);
count++;
break;
}
}
if (count + 1 == deadLineList.Count())
break;
}
return deadLineSequence;
}
}
}