Hashtag how to solve a Leetcode problem.
You may see a lot of YouTubers that are excellent Leetcode problem solvers. They just type away and you are wondering how you get there, well following are the best practices that I gathered from years of experience ;)
- (!) start with a piece of paper and a pen.
This is the most important one. How are you able to implement an algorithm, if you cannot go through it with a simple example on the paper?
- if you cannot think of a solution, can you think of a partial solution that gets you halfway to the goal? Example: https://leetcode.com/problems/rotate-image/, did you think about transposing the matrix first?
- go through your idea with a simple example. Did you cover all edge cases?
- calculate the complexity. If it is a hard problem, is the complexity of your algorithm under O(n * log n)?
- write down your algorithm as a list of steps as comments in your code, maybe in pseudo-code.
- feel free to use an ide (e.g. IntelliJ for Java Developer) and use parameterized tests to implement your algorithm tdd. Also feel free to set some debug points to one of your failing tests.
- paste your code to Leetcode and submit, if it fails, go back to step 1 to check everything again
for () {
// 1:
// 2:
// 3:
// ...
// feel free to print out some important variables :)
}
Happy coding ;)
Top comments (0)