When you’re working on a challenging LeetCode problem, it’s easy to spend a lot of time on it. As long as you can keep thinking of ideas to try, you can keep adjusting your code to see if it improves your results. If your solution fails on large test cases, you can try to optimize it. If it passes most test cases, you can use the first failed case for testing. Hours can go by as you work on your last bug.
As I wrote in the previous tip, this isn’t a good use of study time. Rather than trying to fix every bug, it’s best to stop after an hour or two and look up what you’re missing. Then you can focus your efforts on learning that missing piece.
But while it’s good to minimize how much time you spend on a problem the first time around, multiple subsequent repetitions aren’t necessarily something to avoid. When you find a high-quality problem that covers topics you want to learn, there’s nothing wrong with putting time into it. The key is to spend time in the right areas.
When you’re repeating a problem for the second or third time, your goal should be the same as it was the first time around. Not to see if you can solve the problem with no help, but rather to figure out what skills this problem requires and make sure you learn them thoroughly. During subsequent repetitions, you have the advantage that you have already seen the solution once, so you are reinforcing skills rather than seeing them for the first time. But you can still get stuck. If that happens, consult your model solution to get back on track.
Since your model solution gives you a simple process to get unstuck, you don’t have to spend any more time than necessary when repeating a problem. But what isn’t as predictable is how long it will take you to completely learn a problem. You may find parts of the solution to be challenging to understand, even after you have solved the problem a few times. You may need to read up on math or algorithms. But understanding is the key to learning a solution for the long term. So it’s worth spending as much time as necessary to build this understanding.
This year, I’m publishing a series of tips for effective LeetCode practice. To read the tips in order, start with A Project for 2023.