Skip to content

Commit 44753a8

Browse files
Add files via upload
1 parent e28071d commit 44753a8

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

Heaps and Maps/Activity Selection.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
int Solution::solve(vector<int> &A, vector<int> &B) {
2+
vector<pair<int,int> >ans;
3+
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
4+
for(int i=0;i<A.size();i++)
5+
q.push(make_pair(B[i],A[i]));
6+
auto it = q.top();
7+
int start = it.second;
8+
int end = it.first;
9+
q.pop();
10+
ans.push_back(make_pair(start,end));
11+
while(!q.empty()){
12+
auto itr = q.top();
13+
q.pop();
14+
if(itr.second >= end){
15+
start = itr.second;
16+
end = itr.first;
17+
ans.push_back(make_pair(start,end));
18+
}
19+
}
20+
return (int)(ans.size());
21+
}

Heaps and Maps/Job Sequencing.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
struct Job {
2+
int dead;
3+
int profit;
4+
};
5+
6+
bool compare(const Job &a, const Job &b) {
7+
return (a.profit > b.profit);
8+
}
9+
10+
int solveit(vector<int> &A,vector<int> &B){
11+
int n=A.size();
12+
Job a[n];
13+
for(int i=0; i<n; ++i){
14+
a[i].dead=A[i];
15+
a[i].profit=B[i];
16+
}
17+
sort(a,a+n,compare);
18+
int result[n];
19+
bool slot[n];
20+
for (int i=0; i<n; i++)
21+
slot[i] = false;
22+
for(int i=0; i<n; i++) {
23+
for (int j=min(n, a[i].dead)-1; j>=0; j--) {
24+
if(slot[j]==false) {
25+
result[j] = i;
26+
slot[j] = true;
27+
break;
28+
}
29+
}
30+
}
31+
int ans=0;
32+
for(int i=0; i<n; i++)
33+
if(slot[i])
34+
ans+=a[result[i]].profit;
35+
36+
return ans;
37+
}
38+
39+
int Solution::solve(vector<int> &A, vector<int> &B) {
40+
return solveit(A,B);
41+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
int Solution::solve(vector<vector<int> > &A, int B) {
2+
int n=A.size();
3+
int m=A[0].size();
4+
priority_queue<int> q;
5+
for(int i=0;i<n;i++){
6+
for(int j=0;j<m;j++){
7+
if(q.size()<B)
8+
q.push(A[i][j]);
9+
else{
10+
if(A[i][j]<q.top()){
11+
q.pop();
12+
q.push(A[i][j]);
13+
}
14+
}
15+
}
16+
}
17+
return q.top();
18+
}

Heaps and Maps/Task Scheduler.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
int Solution::solve(string A, int B) {
2+
unordered_map<char, int> record;
3+
int max_freq = 0;
4+
int max_tasks = 0;
5+
for(auto a : A) {
6+
record[a]++;
7+
max_freq = max(max_freq, record[a]);
8+
}
9+
for (auto task : record)
10+
if (task.second == max_freq)
11+
max_tasks++;
12+
int ans = (max_freq - 1) * (B+1) + max_tasks;
13+
return ans > A.size() ? ans : A.size();
14+
}

0 commit comments

Comments
 (0)