leetcode(c++)(扫描线)
#include <iostream> #include <vector> #include <unordered_map> #include <queue> #include <algorithm> using namespace std; bool cmp(pair<int,int>&p1,pair<int,int>&p2) { if(p1.first == p2.first)return p1.second < p2.second; return p1.first < p2.first; } int countAirplanes(const vector<vector<int>>& airplanes) { int n = airplanes.size(); vector<pair<int,int>>list(n * 2); int cnt9 = 0; for(int i = 0; i < n ;++i) { ++cnt9; list.push_back(pair<int,int>(airplanes[i][0],1)); list.push_back(pair<int,int>(airplanes[i][1],-1)); } sort(list.begin(),list.end(),cmp); int cnt = 0; int res = 0; for(auto airplane:list) { if((airplane.first+airplane.second) == 0)continue; if(airplane.second == 1) { ++cnt; } else { --cnt; } res = max(cnt,res); } return res; } bool canAttendMeeting(vector<vector<int>>& meeting) { sort(meeting.begin(),meeting.end()); for(int i = 0; i < meeting.size(); ++i) { if(meeting[i][1] > meeting[i+1][0])return false; } return true; } int minMeetingRooms(vector<vector<int>>& meetings) { int n = meetings.size(); vector<pair<int,int>>rooms; for(int i = 0; i < n; ++i) { rooms.push_back(pair<int,int>(meetings[i][0],1)); rooms.push_back(pair<int,int>(meetings[i][1],-1)); } sort(rooms.begin(),rooms.end(),cmp); int cnt = 0, res = 0; for(auto room:rooms) { if(room.first + room.second == 0)continue; cnt += room.second; res = max(res,cnt); } return res; } bool cmp1(pair<int,int>p1,pair<int,int>p2) { return p1.first < p2.first; } vector<pair<int,int>>merge(vector<pair<int,int>>& intervals) { vector<pair<int,int>>res; int n = intervals.size(); if(n == 0)return res; sort(intervals.begin(),intervals.end(),cmp1); auto cur = intervals[0]; for(auto next : intervals) { if(cur.second >= next.first)cur.second = max(cur.second,next.second); else { res.push_back(cur); cur = next; } } res.push_back(cur); return res; } void print(const vector<pair<int,int>>& vs) { for(auto v:vs) { cout << v.first << " " << v.second << endl; } } void print(const vector<vector<int>>& nums) { for(auto vs:nums) { for(auto num : vs) { cout << num << " "; } cout << endl; } } vector<vector<int>>insert(const vector<vector<int>>& intervals,vector<int>& newInterval) { vector<vector<int>>res; for(auto cur : intervals) { if(newInterval.empty() || cur[1] < newInterval[0]) { res.push_back(cur); } else if(cur[0] > newInterval[1]) { res.push_back(newInterval); res.push_back(cur); newInterval = {}; } else { newInterval[0] = min(cur[0],newInterval[0]); newInterval[1] = max(cur[1],newInterval[1]); } } if(!newInterval.empty())res.push_back(newInterval); return res; } vector<vector<int>>removeIntervals(const vector<vector<int>>&intervals,const vector<int>&remove) { vector<vector<int>>res; for(auto cur : intervals) { if(cur[0] >= remove[1] || cur[1] <= remove[0]) { res.emplace_back(cur); } else { if(cur[0] < remove[0]) { res.push_back({cur[0],remove[0]}); } if(cur[1] > remove[1]) { res.push_back({remove[1],cur[1]}); } } } return res; } int eraseOverlapIntervals(vector<vector<int>>& intervals) { if(intervals.empty())return 0; sort(intervals.begin(),intervals.end()); int cnt = 0,end = INT_MIN; for(auto cur : intervals) { if(end <= cur[0])end = cur[1]; else ++cnt; } return cnt; } int removeCoveredIntervals(vector<vector<int>>& intervals) { sort(intervals.begin(),intervals.end()); int cnt = 0, end = 0; for(auto cur : intervals) { if(end < cur[1]) { end = cur[1]; ++cnt; } } return cnt; } vector<int>minAvailableDuration(vector<vector<int>>&slots1,vector<vector<int>>&slots2,int duration) { sort(slots1.begin(),slots1.end()); sort(slots2.begin(),slots2.end()); int n1 = slots1.size(), n2 = slots2.size(),i = 0, j = 0; while(i < n1 && j < n2) { int commonStart = max(slots1[i][0],slots2[j][0]); int commonEnd = min(slots1[i][1],slots2[j][1]); if((commonEnd - commonStart) >= duration) { return {commonStart,commonStart + duration}; } else if(slots1[i][1] < slots2[j][1])++i; else ++j; } return {}; } void print(const vector<int>& nums) { for(auto num : nums) { cout << num << " "; } cout << endl; } vector<vector<int>>intervalIntersection(const vector<vector<int>>&A,const vector<vector<int>>&B) { vector<vector<int>>res; int m = A.size(), n = B.size(),i = 0, j =0; while(i < m && j < n) { int low = max(A[i][0],B[j][0]); int high = min(A[i][1],B[j][1]); if(low <= high) { res.push_back({low,high}); } if(A[i][1] < B[j][1]) ++i; else ++j; } return res; } bool cmp(vector<int> a, vector<int> b) { return a[0] < b[0]; } vector<vector<int>> employeeFreeTime(vector<vector<vector<int>>> schedule) { vector<vector<int>> v; int n = schedule.size(); for (int i = 0; i < n; i++) { for (int j = 0; j < schedule[i].size(); j++) { v.push_back(schedule[i][j]); } } sort(v.begin(), v.end()); vector<vector<int>> ret; vector<int> temp = v[0]; for (int i = 0; i < v.size(); i++) { if (temp[0] < v[i][1]) { if(temp[1] <= v[i][0]) { ret.push_back({temp[1], v[i][0]}); } temp = v[i]; } else { temp = temp[1] < v[i][1] ? v[i] : temp; } } return ret; } vector<vector<int>> getSkyline(vector<vector<int>>& buildings) { auto cmp = [](const pair<int,int>& a,const pair<int,int>& b)->bool {return a.second - b.second;}; priority_queue<pair<int,int>,vector<pair<int,int>>,decltype(cmp)> que(cmp); vector<int>boundaries; for(auto& building:buildings) { boundaries.emplace_back(building[0]); boundaries.emplace_back(building[1]); } sort(boundaries.begin(),boundaries.end()); vector<vector<int>>res; int n = buildings.size(), idx = 0; for(auto& boundary : boundaries) { while(idx < n && buildings[idx][0] <= boundary) { que.emplace(buildings[idx][1],buildings[idx][2]); ++idx; } while(!que.empty() && que.top().first <= boundary) { que.pop(); } int maxn = que.empty() ? 0 : que.top().second; if(res.size() == 0 || maxn != res.back()[1]) { res.push_back({boundary,maxn}); } } return res; } int main() { // ✈️起飞 vector<vector<int>>airplanes{{1,4},{2,6},{3,7},{4,5}}; cout << countAirplanes(airplanes) << endl; //LeetCode252 vector<vector<int>>meetings{{0,30},{5,10},{15,20}}; // meetings = {{7,10},{2,4}}; cout << canAttendMeeting(meetings) << endl; //LeetCode253 cout << minMeetingRooms(meetings) << endl; //LeetCode56 vector<pair<int,int>>intervals{{1,3},{2,6},{8,10},{9,18}}; print(merge(intervals)); //LeetCode57 vector<vector<int>>intervalss{{1,3},{6,9}}; vector<int>newInterval{2,5}; print(insert(intervalss,newInterval)); //LeetCode1272 vector<vector<int>>intervals0{{0,2},{3,4},{5,7}}; vector<int>toBeRemoved{1,6}; print(removeIntervals(intervals0,toBeRemoved)); //LeetCode435 vector<vector<int>>intervals1{{1,2},{3,4},{1,3},{2,3}}; cout << eraseOverlapIntervals(intervals1) << endl; //LeetCode1288 intervals0 = {{1,4},{3,6},{2,8}}; cout << removeCoveredIntervals(intervals0) << endl; //LeetCode1229 vector<vector<int>>slots1{{10,50},{60,120},{140,210}}; vector<vector<int>>slots2{{0,15},{60,70}}; int duration = 8; print(minAvailableDuration(slots1,slots2,duration)); //LeetCode986 vector<vector<int>>A{{0,2},{5,10},{13,23},{24,25}}; vector<vector<int>>B{{1,5},{8,12},{15,24},{25,26}}; print(intervalIntersection(A,B)); //LeetCode759 vector<vector<vector<int>>>schedule{{{1,2},{5,6}},{{1,3}},{{4,10}}}; print(employeeFreeTime(schedule)); //LeetCode218 vector<vector<int>>buildings{{2,9,10},{3,7,15},{5,12,12},{15,20,10},{19,24,8}}; print(getSkyline(buildings)); return 0; }