forked from WadekarPrashant/LeetCode-Problems-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
6may.cpp
29 lines (26 loc) · 729 Bytes
/
6may.cpp
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
#include<bits/stdc++.h>
class Solution{
public:
int maxCoins(int n,vector<vector<int>> &ranges){
// Code here
sort(ranges.begin(),ranges.end());
vector<pair<int,int>> v(n);
int mx = 0;
for(int i= n-1;i>=0;i--)
{
mx = max(mx,ranges[i][2]);
v[i] = {ranges[i][0],mx};
}
int ans = 0;
for(int i=0;i<n;i++)
{
pair<int,int> p = {ranges[i][1],0};
auto it = lower_bound(v.begin()+i+1,v.end(),p) - v.begin();
if(it<n )
ans = max(ans, ranges[i][2]+v[it].second);
else
ans = max(ans,ranges[i][2]);
}
return ans;
}
};