ARTS check-in for the fourth week, feel at ease

introduction

Time flies so fast, it’s already the fourth week of learning and checking in, and it’s also the last week of this event. Anyone who knows Shopkeeper 3 must know that I have been creating technical blogs for 6 years, and regularly publish no less than 6 blog posts every month. At the same time, as a developer who loves sharing, activities like ARTS are naturally indispensable to me. Since I plan to share them together, I have done local document recording before, so I can just integrate the content, and then I will start my fourth week check-in.

Algorithm

This week I want to share a more classic algorithm question, which is also a classic algorithm question of Leetcode-449 ( LeetCode official website - a technology growth platform loved by geeks around the world ): Serialization and Deserialization 2 Fork search tree problem.

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted over a network connection link for later reconstruction in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary search tree. There are no restrictions on how the serialization/deserialization algorithm works. You just need to make sure that the binary search tree can be serialized to a string, and that the string can be deserialized into the original binary search tree.

The encoded string should be as compact as possible.

Idea : Given a binary tree, "pre-order traversal" and "in-order traversal" can restore the binary tree. Given a binary tree, "post-order traversal" and "in-order traversal" can also restore the binary tree. For binary search trees, given "pre-order traversal" or "post-order traversal", "in-order traversal" can be obtained by sorting them. Therefore, serialization and deserialization requirements can be achieved by only performing "pre-order traversal" or "post-order traversal" on the binary search tree. This problem is solved using the "post-order traversal" method.

When serializing, you only need to perform post-order traversal of the binary search tree and then encode the array into a string.

When deserializing, the string needs to be decoded into an array for post-order traversal. When restoring the array traversed in post-order into a binary search tree, there is no need to sort the array traversed in in-order and then restore the binary tree based on the arrays traversed in in-order and post-order. Instead, the array can be traversed directly in post-order according to the order. An array of restored binary search trees. In the array obtained by post-order traversal, the value of the root node is at the end of the array, the nodes of the left subtree are all smaller than the value of the root node, and the nodes of the right subtree are all larger than the value of the root node. A recursive function can be designed based on these properties to restore the second Fork search tree.

The specific JS implementation code is as follows:

var serialize = function(root) {
    const list = [];

    const postOrder = (root, list) => {
        if (!root) {
            return;
        }
        postOrder(root.left, list);
        postOrder(root.right, list);
        list.push(root.val);
    }

    postOrder(root, list);
    const str = list.join(',');
    return str;
};

var deserialize = function(data) {
    if (data.length === 0) {
        return null;
    }
    let arr = data.split(',');
    const length = arr.length;
    const stack = [];
    for (let i = 0; i < length; i++) {
        stack.push(parseInt(arr[i]));
    }

    const construct = (lower, upper, stack) => {
        if (stack.length === 0 || stack[stack.length - 1] < lower || stack[stack.length - 1] > upper) {
            return null;
        }
        const val = stack.pop();
        const root = new TreeNode(val);
        root.right = construct(val, upper, stack);
        root.left = construct(lower, val, stack);
        return root;
    }

    return construct(-Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, stack);
};

Complexity analysis:

Time complexity: O(n)O(n)O(n), where nnn is the number of nodes in the tree. serialize\textit{serialize}serialize requires O(n)O(n)O(n) time to traverse each point. deserialize\textit{deserialize}deserialize requires O(n)O(n)O(n) time to restore each point.

Space complexity: O(n)O(n)O(n), where nnn is the number of nodes in the tree. serialize\textit{serialize}serialize requires O(n)O(n)O(n) space to save the value of each point in an array, and the deepest recursion depth is O(n)O(n)O(n). deserialize\textit{deserialize}deserialize requires O(n)O(n)O(n) space to save the value of each point in an array, and the deepest recursion depth is O(n)O(n)O(n).

Review

This week I share an English article that I am learning about blockchain. The article is about how Bitcoin actually works. The article link is How the Bitcoin protocol actually works – DDI . From here on, this article goes into more technical perspectives to understand Some technical details of Bitcoin, but still to understand the Bitcoin protocol framework as a whole and to be familiar with the technical paradigm of blockchain projects represented by Bitcoin.

Here are some excerpts for sharing:

A problem with the first version of Infocoin is that Alice could keep sending Bob the same signed message over and over. Suppose Bob receives ten copies of the signed message “I, Alice, am giving Bob one infocoin”. Does that mean Alice sent Bob ten different infocoins? Was her message accidentally duplicated? Perhaps she was trying to trick Bob into believing that she had given him ten different infocoins, when the message only proves to the world that she intends to transfer one infocoin.

What we’d like is a way of making infocoins unique. They need a label or serial number. Alice would sign the message “I, Alice, am giving Bob one infocoin, with serial number 8740348”. Then, later, Alice could sign the message “I, Alice, am giving Bob one infocoin, with serial number 8770431”, and Bob (and everyone else) would know that a different infocoin was being transferred.

One problem with the first version of Infocoin was that Alice might keep sending the same signed message to Bob. Suppose Bob receives ten pieces of signed information "I, Alice, give Bob an information coin". Does this mean that Alice sent ten different information coins to Bob? Was her information accidentally copied? Perhaps she was trying to trick Bob into believing that she gave him ten different infocoins, and this message only proved to the world that she intended to transfer one infocoin.

What we want is a way to make Infocoin unique. They require a tag or serial number. Alice will sign the message: "I, Alice, give Bob an information coin with the serial number 8740348." Alice can then sign the message "I, Alice, give Bob an infocoin with serial number 8770431" and Bob (and others) will know that another infocoin is being transferred.

Technique/Tips

This time I will share an important knowledge point about front-end development. In the front-end development process, it involves algorithm-related knowledge and the use of greedy algorithm-related knowledge points. Here we share a practical scenario in front-end development about the best time to buy and sell stocks. This is not an algorithm question. This is indeed a knowledge point that can be used in front-end development. The specific implementation code is as follows:

/**
 
* @param {number[]} p 价格
 
* @return {number} r 最优解
 
*/
 
var maxOptimal = function(p) {
 
    var r = 0;
 
    for(var i=1;i<=p.length;i++){
 
        if(p[i]>p[i-1]){
 
            r = p[i] - p[i-1] + r;
 
        }
 
    }
 
    return r;
 
};

Share

This time I will share the topic of whether programmers need to be certified. Different people have different views on this topic. Some people think that programmers only need to have a solid programming foundation, rich project experience and an attitude of continuous learning to be qualified for the job; while some people think that programmers need to obtain professional certificates. , Only in this way can you prove that you have certain professional skills and abilities, and you can also get better job opportunities and salary benefits, and improve your competitive advantage.

In actual situations, in most companies, the career development of programmers needs to take into account many factors, such as professional knowledge, skill level, project experience, etc. Certification is undoubtedly one way to measure a programmer's professionalism, but it is not the only way. If a programmer has rich project experience, excellent programming skills and continuous learning of new technologies, he can also be recognized by the company and have opportunities for career advancement.

Therefore, I personally think that programmers can go for verification, but it is not a necessary choice. Everything is determined by their own actual situation.

Conclusion

As of this writing, I feel comfortable in the fourth week and have completely adapted to the ARTS check-in learning mode. Generally speaking, I use a week of spare time to study these four things respectively. Generally speaking, I spend some time every day to study these four things respectively. Then there is a summary at the end of the week. This method is also quite good. As of the fourth week, the ARTS learning month that I participated in has come to an end, but I will not end with the end of the activity. I will continue to improve myself a little bit every week in this way, maintain a state of continuous learning, and let myself continue to learn. Continuous progress and growth in a subtle way!

Guess you like

Origin blog.csdn.net/CC1991_/article/details/133134430