My Way to Learn Algorithm

Translator: ShuozheLi Author: labuladong​

I have published an article about the ideal framework. People from the community have give praised me. I have never thought so many people will be 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 Algorithm. Especially those who are beginner 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 already start 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 just like software engineering. People like me who never lead 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 Algorithm, I am always having knowledge gaps in my mind. If we summary 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 function in these data type. You do not even need to writer a new operation for them.

2.what is that for?

This problem covers the design of Algorithm. 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 has said. Algorithm is just about how to use data structure. The frequent Algorithm question are 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 the thickness of the book because you only need to read two-third 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 is 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 Algorithm. Do not just think you can understand them without doing it. But, if you know the basics, you can jump the beginning. You can also jump the math proving part and practice part. This shall make the book less thick.

There is a trick to read a book. You should not be stoped by details. You need to build a big knowledge framework first. keep move 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 linearly relationship between Algorithm and Math ability and program language does not really matter. 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 companys ask them.

For the beginner, you should go to the "Explore" in 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 you start with "Explore" or "Problems". You better practice problems by types. For example, you can finish all the problem in the linked-list then jump to 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 strong desire!! Let me take myself as an example

Half-year ago I start to practice problems for a job after graduation. But, most of the 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 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 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 by your eye.

Also, as a person who likes to share, I find out 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 internet.

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

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