TLE because of a cout 😭
Was solving LeetCode 2126 (Destroying Asteroids) with a simple greedy approach:
- Sort asteroids
- Absorb from smallest to largest
Complexity was O(n log n), so I couldn't understand why I was getting:
>
After checking the logic multiple times, I finally noticed this line inside my loop:
cout << mass << " " << asteroids[i] << endl;
I removed it and...
>
The algorithm was correct all along. The debug print was causing the TLE.
Today's lesson:
>
u/FirmAd3947 — 10 days ago