
- 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
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 Articles
- 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 minimum number of steps required to catch the opponent in C++
- Program to find number of minimum steps required to meet all person at any cell 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 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
- Minimum number of operations required to delete all elements of the array using C++.
- Program to find minimum number of operations required to make lists strictly Increasing in python
- C++ program to find minimum number of locations to place bombs to kill all monsters
- Program to find minimum number of swaps needed to arrange all pair of socks together in C++
- Program to find minimum number of operations required to make one string substring of other in Python

Advertisements