DEV Community

geyuqiu
geyuqiu

Posted on • Updated on

Leetcode 102 best practices

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 ;)

  1. (!) 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?
  2. go through your idea with a simple example. Did you cover all edge cases?
  3. calculate the complexity. If it is a hard problem, is the complexity of your algorithm under O(n * log n)?
  4. write down your algorithm as a list of steps as comments in your code, maybe in pseudo-code.
  5. 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.
  6. 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 :)  
}
Enter fullscreen mode Exit fullscreen mode

Happy coding ;)

Top comments (0)