
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum Number of Events That Can Be Attended in C++
Suppose we have an array of events where events[i] = [startDayi, endDayi]. Here every event I start at startDayi and ends at endDayi. We can attend an event I at any day d where d in range startTimei and endTimei (both inclusive). We have to keep in mind that we can only attend one event at any time. So find the maximum number of events we can attend. So for example, if the input is like [[1,4], [4,4], [2,2], [3,4], [1,1]], then the output will be 1, as we can attend maximum of four events, these are [1, 1], [2, 2], [3, 4] and [4, 4].
To solve this, we will follow these steps −
n := number of events, then sort the event list based on the start date, set ret := 0 and itr := 0
create a priority queue based on max-heap called pq
for I in range 1 to 10000
while itr < n and events[itr, 0] = i
insert events[itr, 1]
increase itr by 1
while pq is not empty and top of pq < i,
delete element from pq
if pq is not empty, then delete from pq and increase ret by 1
return ret
Example (C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: static bool cmp(vector <int>& a, vector <int>& b){ return a[0] < b[0]; } int maxEvents(vector<vector<int>>& events) { int n = events.size(); sort(events.begin(), events.end(), cmp); int ret = 0; int itr = 0; priority_queue <int, vector <int>, greater <int>> pq; for(int i = 1; i <= 1e5; i++){ while(itr < n && events[itr][0] == i){ pq.push(events[itr][1]); itr++; } while(!pq.empty() && pq.top() < i) pq.pop(); if(!pq.empty()){ pq.pop(); ret++; } } return ret; } }; main(){ vector<vector<int>> v = {{1,4},{4,4},{2,2},{3,4},{1,1}}; Solution ob; cout << (ob.maxEvents(v)); }
Input
[[1,4],[4,4],[2,2],[3,4],[1,1]]
Output
4
- Related Articles
- Maximum number of candies that can be bought in C
- Find maximum number that can be formed using digits of a given number in C++
- Maximum number of people that can be killed with strength P in C++
- Maximum number of threads that can be created within a process in C
- Maximum number of partitions that can be sorted individually to make sorted in C++
- Program to find maximum number of package that can be bought by buyers in C++
- Problem to Find Out the Maximum Number of Coins that Can be Collected in Python
- C++ program to find out the maximum number of cells that can be illuminated
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- Maximum number of 2×2 squares that can be fit inside a right isosceles triangle in C
- Maximum number that can be display on Seven Segment Display using N segments in C++
- Maximum money that can be withdrawn in two steps in C
- Maximum number of segments that can contain the given points in C++
- Maximum litres of water that can be bought with N Rupees in C++
- Maximum possible time that can be formed from four digits in C++
