Writing your own model solution for a LeetCode problem can help you clarify the solution details in your mind, and it leaves you with a living document to use and improve each time you practice the problem. As an example of what to include in a model solution, I’ll use the one I wrote for LeetCode 1818: Minimum Absolute Sum Difference.
Here are the sections I recommend including in a model solution:
Problem
Rewrite the problem statement in a way that makes sense to you. LeetCode problem statements aren’t always written in the most understandable way, and even if a problem statement is clear to some readers, it may not be clear to you. So write your own. This section is also a good place to introduce variables you can refer to in the solution.
Solution
Now that you clearly understand the problem, write the solution itself.
Intuition
Summarize the key challenges posed by the problem, and the main approach you will use to solve it. When you’re writing the more detailed sections below, make sure they follow the general plan explained in this section. If they don’t, either adjust the details or adjust the plan. You can also use the intuition section to remind yourself how to solve the problem if you forget what approach you used in a future practice session. In this way, it’s a bit like the hints that LeetCode provides for some problems. But while LeetCode authors write hints cryptically, so they don’t give away too much, you should write your intuition section clearly.
Algorithm ideas
In LeetCode discussion posts, authors often jump to the solution without explaining how they figured it out. The algorithm ideas section is for explaining how you got from the intuition to the final product, including any false starts that can help explain why you solved the problem in the way you did.
Algorithm
LeetCode problems focus on algorithms and data structures. The algorithm section explains the final algorithm that you used to solve the problem. Usually, you’ll want to explain the algorithm using pseudocode. There is no official pseudocode syntax like there is for a programming language, so use whatever form of pseudocode is most understandable to you. For the Minimum Absolute Sum Difference solution, I used a bulleted list, with the statements inside the for loop indented as they would be in code. For a more code-like pseudocode format, see my model solution for LeetCode 227: Basic Calculator II.
Algorithm Notes
For some problems, it may not be clear why the algorithm works, even when the explanation is well written. Example cases or test data can help clarify how the algorithm handles different input. For an example of explaining why an algorithm works by enumerating all cases, see my model solution for LeetCode 1288: Remove Covered Intervals. This model solution also uses diagrams, which make most model solutions more understandable.
Code
Finally, we have the actual code, meaning code that LeetCode would accept if you submitted it. If you used code ideas from other sources, this is a good place to link to those in case you want to look them up later. Although LeetCode doesn’t care about code comments, it’s useful to add them to your model code to show where you implement each step of the solution. If there are any remaining questions about what each code block does and why it works, this is the place to clear them up.
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.