-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimalMergePattern.cpp
More file actions
57 lines (46 loc) · 855 Bytes
/
OptimalMergePattern.cpp
File metadata and controls
57 lines (46 loc) · 855 Bytes
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
#include <bits/stdc++.h>
using namespace std;
class OpMergePattern
{
private:
priority_queue<int, vector<int>, greater<int>> fileMin; // min-heap priority queue
int jobSlot;
int maxProfit = 0;
public:
void takeInput();
int getMinMergeCost();
void printArray();
};
void OpMergePattern::takeInput()
{
int _size;
cout << "Enter number of files:";
cin >> _size;
int file_size;
while (_size--)
{
cin >> file_size;
fileMin.push(file_size);
}
}
int OpMergePattern::getMinMergeCost()
{
int count = 0;
while (fileMin.size() > 1)
{
int newMerged = fileMin.top();
fileMin.pop();
newMerged += fileMin.top();
fileMin.pop();
count += newMerged;
fileMin.push(newMerged);
}
return count;
}
int main()
{
OpMergePattern *demo1 = new OpMergePattern();
demo1->takeInput();
cout << demo1->getMinMergeCost() << endl;
return 0;
}