James Tsang

James Tsang

A developer.
github
twitter
tg_channel

ARTS Check-in Day 3

A: 257. Binary Tree Paths#

Given the root of a binary tree, return all root-to-leaf paths in any order.
A leaf is a node with no children.

paths-tree

Input: root = [1,2,3,null,5]
Output: ["1->2->5","1->3"]

The problem may be relatively simple, or maybe I've been doing algorithm questions for two days and have gained some momentum. This time it was relatively quick, passed on the first try. The following is the submitted code:

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */
function binaryTreePaths(root: TreeNode | null): string[] {
  const result: string[] = []
  const dfs = (node: TreeNode | null, path: string) => {
    if (!node || node.val === null) {
      return
    }
    const currentPath = `${path}${path ? '->' : ''}${node.val}`
    if (!node.left && !node.right) {
      result.push(currentPath)
    }
    dfs(node.left, currentPath)
    dfs(node.right, currentPath)
  }
  dfs(root, '')
  return result
};

The result of this submission is:

Your runtime beats 93.55 % of typescript submissions
Your memory usage beats 53.22 % of typescript submissions (44.2 MB)

The basic idea of depth-first search (DFS) is to first visit a node, then try to visit its connected nodes, and so on, until there are no unvisited nodes left. Recursion is a very common method for implementing DFS.

The core idea of the standard DFS algorithm is:

  • Recursively explore each node.
  • Mark the visited nodes to avoid duplicate visits.
  • Traverse all adjacent nodes.

The special nature of the binary tree problem above is that only the left and right child nodes of the binary tree need to be considered, and there is no need to mark the nodes because the tree structure guarantees that there are no cyclic paths.

R: Productivity#

This article is written by Sam Altman, the President of OpenAI. He wrote this article to explain his views on productivity because he is often asked about it. Here are some of his thoughts and experiences:

Compound interest is not only a financial concept, but also a career concept. Career compound interest is like magic.

About What to Do#

It is meaningless to go fast in the wrong direction. The most important thing for productivity is to do the right things, which is often overlooked. Independent thinking is difficult but can be improved with practice.

Impressive people have a strong sense of conviction, which is lacking in most people. This also means courage, the kind of conviction that allows you to dare to oppose and be different from most people. Even if you make mistakes sometimes, don't let go of your conviction, because making mistakes is normal.

Make sure to leave enough time in your schedule to think about what to do. Sam's way is to read, chat with interesting people, and spend time in nature.

Efficiency is low when doing things you don't like. Sam tries to delegate or avoid these things as much as possible. A good way to delegate is to find people who enjoy doing these things, as they can do them better and more efficiently. Doing things you don't like will drag down your morale and motivation.

It is important to know that you can learn anything you want to learn, and you can progress quickly. At first, you may think it's a miracle, but eventually, if you persist, you will find that you can do it.

Great work usually requires collaboration with colleagues. Working with smart, efficient, happy, and positive people will not undermine your ambition, while working with the opposite will come at a great cost.

About Priorities#

The priority system has three principles: 1. Make sure to complete important things; 2. Don't waste time on stupid things; 3. Make many to-do lists.

Making many to-do lists helps in multitasking without having to keep these things in mind. When not involved in a task, you can quickly find something interesting to do.

Determine priorities in a way that generates motivation: the more you do, the better you feel, and the better you feel, the more you can do. Sam likes to start and end each day in a way that makes progress.

Try to avoid meetings. 90% of meetings are a waste of time, and the remaining 10% can make up for the content of these meetings. The best duration for a meeting is between 15-20 minutes or a long meeting of two hours.

Different time periods of the day are suitable for different tasks. The first few hours of the morning are the most efficient, and no one should schedule anything during that time. Meetings are scheduled for the afternoon. If your attention starts to decline, take a break and switch to another task.

Most people don't value their time enough - a person who can earn $100 per hour spends several hours doing something they don't like to save $20.

Don't fall into the productivity trap. Many people think about how to optimize their systems without thinking about whether they are addressing the right problems. It doesn't matter how good your system is if you are doing the wrong things. The correct goal is to optimize your year, not just your day.

About Physical Factors#

Keep your body in good condition. Strategies may vary from person to person, but for Sam, doing the following things feels like at least a 1.5x increase in productivity (I didn't record some habits that are not universal):

  • Sleep is the most important factor in maintaining productivity. Sleep trackers can help find the best sleep strategy. An easy-to-maintain strategy is to set it and forget it, such as Emfit QS+Active.
  • Sleep in a cool, dark, and quiet environment. A good mattress can greatly improve sleep quality.
  • Avoid eating too much and drinking alcohol before bed.
  • Use an eye mask and earplugs when traveling.
  • Exercise may be another important factor in maintaining physical condition. After trying different exercise programs for a few months, the best program seems to be weightlifting for an hour three times a week, with occasional high-intensity interval training. In addition to improving productivity, this is also the exercise program that makes him feel the best overall.
  • The third important factor is nutrition. Consuming a lot of sugar should be avoided, as well as spicy and stimulating foods. Because he has no resistance to sweets, he tries not to buy them.

Others#

Ensure a work environment with natural light, quietness, and no distractions.

Write custom software for frequent trivial tasks. Learn how to type faster and use keyboard shortcuts to complete work.

Avoid contact with people and things that will cause bad moods.

For planned tasks, try to exceed a little each time at the same time. This can make you feel efficient in everything, but excessive overachievement can be disastrous.

Don't neglect your family, friends, and things you enjoy. Otherwise, you will be unhappy and it will also affect your productivity.


To reiterate: it is meaningless to be productive in the wrong direction. It is more important to think clearly about what you want to do.

T: Jupyter Kernel Gateway#

An official project of Jupyter that turns the Jupyter kernel into an API, allowing you to start a service without depending on Jupyter Notebook or JupyterLab.

S: A Reflection on the "Pareto Principle"#

Before taking action, think about what you want, not what you want to do. There are many interesting and challenging things, and time is not enough to do everything. For these things, focus on the part that aligns with what you want, which is in line with the Pareto Principle. If there is energy left, then do what you need to do.


Reference:

  1. ARTS Challenge
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.