My Way to Learn Algorithm

Translator: ShuozheLi Author: labuladong

I have published an article about the ideal framework. People from the community have praised me. I have never thought so many people will agree with me. I will work harder to write more easily understanding Algorithm articles.

Many friends asked me how should I learn Data Structure and Algorithms. Especially those who are beginners feel exhausted when going through all the LeetCode questions even after reading my article about the ideal framework. So, they hope to explain and tell them the way how I begin.

First of all, I want to congrats my friends who are asking me for it because you have learned that you need external help from experienced people. And, you have already started to practice Algorithm problems. Remember, there are not many people made to this step.

For the ideal framework, a beginner may not be easy to understand. But if you are able to understand it, you are not a beginner anymore :D! It is just like software engineering. People like me who never led a group project feel so bored. But, for someone who has led a team. He/she will treat every sentence in software engineering as a treasure. (if you do not understand this, it is fine for now)

Now, I will go through my experience. if you have read many articles such as "how to practice LeetCode" and "how to study Algorithm", but you still cannot keep up. Then, this article is for you.

When I begin to learn Data Structure and Algorithms, I am always having knowledge gaps in my mind. If we summarize them into two questions:

  1. what is it?

  2. what is that for?

For example, if you learned the word stack, your teacher may tell you "first in last out" or "function stack." However, all these are like literature words. They cannot answer your question. Here is how to answer them:

  1. what is it? Go read the textbook or description of its basic elements

  2. what is that for? practice some coding questions

1. what the hell is it?

This question is easy to solve. You just need to read a chapter of the book. And then make your own Queue or Stack. If you can understand the first half of the ideal framework: Data structure is just the combination of array and linked-list. All the operations are just add, remove, search, modify.

For example, Queue is just made by an array or linked-list. For enqueue and dequeue operations are just add and remove functions in these data types. You do not even need to write a new operation for them.

2. what is that for?

This problem covers the design of Algorithms. This will take a long time to make through. You need to practice a lot of questions and train yourself to think like a computer.

The previous article said. The algorithm is just about how to use data structure. The frequent Algorithm question is just a few types. many problems just changed a few conditions. Practice problems just help to see the pattern in questions. With the pattern in your head, you can solve problems with your own framework. Feels like plug in numbers into an equation.

For example, if you need to escape a maze, you need to treat the problem abstractly. maze -> graph search -> tree search -> binary tree search then you just apply your own framework

You need to abstract and categorize the problem when you are practicing LeetCode. This will help you to find the pattern.

3. how to read a book

Let me just recommend a book to you. Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne If you can need 50% of the book, you are at the average level. Do think about the thickness of the book because you only need to read two-thirds of the book.

Now let's talk about how to read it. Reading the book using recursively: from the top to bottom, step by step to divide the problem.

This book has a good structure for beginners, so you can read from the first chapter to the end. Make sure you typed down and run all the code in the book Because these are really just the basic elements of Algorithms. Do not just think you can understand them without doing it. But, if you know the basics, you can skip the beginning. You can also skip the math proving part and practice part. This shall make the book less thick.

There is a trick to reading a book. You should not be stopped by details. You need to build a big knowledge framework first. keep moving on, do not get stuck by details

Well, the ending part of the book is very hard. There are some famous Algorithms such as Knuth–Morris–Pratt algorithm (KMP) and Regular expression. These are not useful for doing LeetCode so you do not have to know them.

4. how to practice problem

  1. there is not a linear relationship between Algorithm and Math ability and program language does not really matter. The algorithm is just a way to think. To think like a computer is just like riding a bike. You have to think you are walking with two wheels, not two feet.

LeetCode problem does not like the classic Algorithm we talked about before. Leetcode problems are more like brain teasers. For example, you need to use a queue to make a stack or oppositely make a stack with a queue. Add two numbers without using add sign.

Although these questions are useless, to solve them you still need a solid understanding of data structure and basic knowledge. This is why the companies ask them.

For the beginner, you should go to the "Explore" in the menu and start on "learn" This will help you to go through the basic data structure and Algorithm. There are lectures and corresponding practice problems.

Recently, the "learn" part added something new like Ruby and Machine learning. You have no need to worry about that. You just need to finish the basic part. Then, you can just go directly to the "Interview" problem part.

No matter whether you start with "Explore" or "Problems". You better practice problems by type. For example, you can finish all the problems in the linked-list and then jump to the binary tree problem. This helps you find the framework and pattern and practice applying them.

5. I know what you said, but I cannot keep up

This is all bout what you really want. You need to activate your desire! !! what I am saying is not a hobby but a strong desire!! Let me take myself as an example

Half-year ago I start to practice problems for a job after graduation. But, most people start it when they almost graduate.

I know I am not smart so I start early. I have a strong desire for a decent job. I have a girl who I want to be in love and I have made boasts in front of my friend. So, I have to achieve it to earn the money the fame I want. The desire for money and for fame has made to work harder and harder.

But, I am the kind of person who does not good at doing things quickly right before the deadline. I understand things slowly. Therefore, I decide to wake up early and start everything ahead. In fact, if you keep focusing on something for only just a month. You can see your improvement with your eye.

Also, as a person who likes to share, I find out that what I said actually helps others. This gives me recognition too! This is also what I want!! So, I decided to write more about what I experienced and share them on WeChat and the internet.

Above, it is not only about Algorithm learning. We, as human beings, are driven by our desires. There must be a thing that is tangible to help us to keep up. We have to benefit directly from it! This should be a simple reason to keep up with what we want to achieve.

You can find me on Wechat official account labuladong: All my friends, labuladong loves you.

Last updated