Im trying to solve the activity selection problem but do not understand how to improve my code to avoid the Time Limit Error (TLE) . Please suggest what went wrong in this simple question .
My code :
#include<algorithm> vector<int> maximumMeetings(vector<int> &start, vector<int> &end) { // Write your code here. vector<vector<int>> A; vector<int> temp ; for(int i=0; i<start.size(); i++){ temp.clear(); temp.push_back(start[i]); temp.push_back(end[i]); temp.push_back(i+1); A.push_back(temp); } sort(A.begin(),A.end(),[](vector<int>A,vector<int>B){ if(A[1]!=B[1]){ return A[1]<B[1]; } else{ return A[2]<B[2]; } }); vector<int> ans; ans.push_back(A[0][2]); int j=0; for(int i=1; i<A.size(); i++){ if(A[i][0]>A[j][1]){ ans.push_back(A[i][2]); j=i; } } return ans ; } EDIT 1 : here is the problem copy pasted from the site , as requested in the comments , tho I have not pasted the example test cases to keep it short .
Problem Statement You are given the schedule of N meetings with their start time Start[i] and end time End[i]. But you have only 1 meeting room. So, you need to tell the meeting numbers you can organize in the given room, such that the number of meetings organized is maximum. Note: The start time of one chosen meeting can’t be equal to the end time of the other chosen meeting. Also for the same end time, a meeting with a smaller index is preferred.
The doubt has been solved , I just needed to pass the vector by reference in the lambda comparator function for sort function .
sortcomparator take it's arguments by reference would be a good starting point for optimisations