Practicing a LeetCode problem with the help of a model solution ensures that you are learning an optimal solution, not just any accepted solution. But don’t just take someone else’s solution as your model solution. Use what others have written, and make it your own.
Once you choose to include a problem in your set of model problems, you can begin writing the model solution. After you solve the problem once, the next step is to do some research. Review what others have written about the problem to see what approaches are available. Most problems are amenable to a few different approaches, but model solutions should focus on just one of them. For an especially good model problem, you could write two separate model solutions using two different approaches. But I would recommend finding a second model problem for practicing the second approach. LeetCode has plenty of problems, and it’s rare to find a single problem that is truly the best choice for learning two different solution patterns.
The solution approach will dictate which algorithms and data structures to use for your solution. Collect a few example solutions that use a similar approach. This will show you how different people implement the same type of solution. You may find that one of these examples meets your needs, but you can often combine the best parts of multiple solutions.
Next, make sure you understand how the solution works, and why each block of code is necessary. Go through your example solutions and draw diagrams, write explanations, and add code comments. These artifacts will be useful for any problem, but depending on the problem type, some of them will be more useful than others. For example, a geometry problem lends itself to pictures, while equations may work better to explain a number theory problem. I find it useful to write up a formal solution (an editorial in competitive programming jargon) like this one I wrote for LeetCode 1818: Minimum Absolute Sum Difference. I think it helps present the solution research in an organized way that helps me remember how it all works when I come back to it later.
Of course, you’ll also need to write your own code for your model solution. It will have some relationship to the example solutions that you found during the research phase, but you’ll write it in your preferred language, using your own unique coding style. And most importantly, you will get the experience of writing a solution from scratch.
Your model solution isn’t done when you have working code and an editorial explaining why your code works. You should practice model solutions repeatedly. Each time you practice, you might notice some code that you can make clearer, or a section of the editorial that isn’t as understandable as it could be. When this happens, update your model solution. A source control system like Git comes in handy here, so you can review your changes and revert if something doesn’t work as expected. Updating your model solution regularly ensures that it captures what you learn each time you practice it, and that older solutions take advantage of lessons from newer ones.
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