I was trying to submit this -> Remove Max Number of Edges to Keep Graph Fully Traversable question.
but I got TLE because of sort using rbegin
sort( edges.rbegin(), edges.rend())
but after change with comparator funtction code got accepted.
sort(edges.begin(), edges.end(), [&](vector <int> &a, vector <int> &b){
return a[0] > b[0];
});
Submission Using rbegin Submission Using comparator
I tried to run this in local and got similar perfomance but when I used some different build command I got 2x tiime difference.
Build command:
g++ a.cpp
g++ -std=c++17 -Wshadow -Wall -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG -DLOCAL a.cpp
Following code I used for benchmarking.
Benchmarking code
int n;
cin >> n;
srand(time(0));
vector <int> ans(n);
for(int i = 0; i < n; ++i){
ans[i] = random() % 100000;
}
vector <int> xyz = ans;
const clock_t begin_time = clock();
sort(ans.begin(), ans.end(), greater <int> ());
std::cout << float( clock () - begin_time ) / CLOCKS_PER_SEC << endl;
const clock_t begin_time2 = clock();
sort(xyz.rbegin(), xyz.rend());
std::cout << float( clock () - begin_time2 ) / CLOCKS_PER_SEC << endl;
I think that leetcode is using similar build command to execute the program. Can anyone explain why this time difference happening?