0

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 .

question : https://www.codingninjas.com/codestudio/problems/1062658?topList=striver-sde-sheet-problems&utm_source=striver&utm_medium=website&leftPanelTab=1

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 .

4
  • Can you also paste the problem statement Commented Jun 27, 2022 at 5:47
  • Questions should be self contained and not rely on external links. What is the code supposed to do? Please show a minimal reproducible example Commented Jun 27, 2022 at 5:53
  • 2
    Making your sort comparator take it's arguments by reference would be a good starting point for optimisations Commented Jun 27, 2022 at 5:56
  • @AlanBirtles yes sir , that solved the TLE , thank you very much , I didnt realise this . Could you please convert your comment to an answer so that I can mark it as accepted for use of others Commented Jun 27, 2022 at 6:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.