- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find minimum number of pins required to hang all banners in C++

Suppose we have a list of intervals of the form [start, end] this is representing the starts and end points of banners we want to hang. At least one pin is required to hang a banner, and one pin can hang more than once banners. We have to find the smallest number of pins required to hang all the banners.

So, if the input is like intervals = [[2, 5],[5, 6],[8, 10],[10, 13]], then the output will be 2, as we can put two pins at position 5 and 10 to hang all of the banners.

To solve this, we will follow these steps −

- sort the array v based on the end values of intervals
- ret := 0
- last := -inf
- for each item it in v −
- if last >= start of it, then −
- Ignore following part, skip to the next iteration

- (increase ret by 1)
- last := end of it

- if last >= start of it, then −
- 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.back() < b.back(); } int solve(vector<vector<int>>& v) { sort(v.begin(), v.end(), cmp); int ret = 0; int last = -1e8; for (auto& it : v) { if (last >= it[0]) { continue; } ret++; last = it[1]; } return ret; } }; int solve(vector<vector<int>>& intervals) { return (new Solution())->solve(intervals); } int main(){ vector<vector<int>> v = {{2, 5},{5, 6},{8, 10},{10, 13}}; cout << solve(v); }

## Input

{{2, 5},{5, 6},{8, 10},{10, 13}}

## Output

2

- Related Questions & Answers
- Program to find minimum number of movie theatres required to show all movies in python
- Program to find minimum number of busses are required to pass through all stops in Python
- Program to find number of minimum steps required to meet all person at any cell in Python
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum number of hops required to reach end position in Python
- Program to find minimum number of buses required to reach final target in python
- Program to find minimum number of flips required to have alternating values in Python
- Program to find minimum number of steps required to catch the opponent in C++
- Program to find minimum number of operations required to make lists strictly Increasing in python
- C++ Program to find out the minimum number of operations required to defeat an enemy
- Minimum number of mails required to distribute all the questions using C++.
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to find minimum number of bricks required to make k towers of same height in Python
- Program to find minimum number of vertices to reach all nodes using Python
- Program to find minimum number of deletions required from two ends to make list balanced in Python

Advertisements