Algorithm
本周选择的算法题是:Simplify Path。
规则
Given a string path
, which is an absolute path (starting with a slash '/'
) to a file or directory in a Unix-style file system, convert it to the simplified canonical path.
In a Unix-style file system, a period '.'
refers to the current directory, a double period '..'
refers to the directory up a level, and any multiple consecutive slashes (i.e. '//'
) are treated as a single slash '/'
. For this problem, any other format of periods such as '...'
are treated as file/directory names.
The canonical path should have the following format:
- The path starts with a single slash
'/'
. - Any two directories are separated by a single slash
'/'
. - The path does not end with a trailing
'/'
. - The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period
'.'
or double period'..'
)
Return the simplified canonical path.
Example 1:
Input: path = "/home/"
Output: "/home"
Explanation: Note that there is no trailing slash after the last directory name.
Example 2:
Input: path = "/../"
Output: "/"
Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.
Example 3:
Input: path = "/home//foo/"
Output: "/home/foo"
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.
Constraints:
1 <= path.length <= 3000
path
consists of English letters, digits, period'.'
, slash'/'
or'_'
.path
is a valid absolute Unix path.
Solution
class Solution:
def simplifyPath(self, path: str) -> str:
stack = []
for item in path.split("/"):
if item == "..":
if stack:
stack.pop()
elif item and item != ".":
stack.append(item)
return "/" + "/".join(stack)
Review
I’m an Engineering Manager who can’t code
文中引用了 Google 研究的 Best Manager 十大行为:
- Is a good coach
- Empowers team and does not micromanage
- Creates an inclusive team environment, showing concern for success and well-being
- Is productive and results-oriented
- Is a good communicator — listens and shares information
- Supports career development and discusses performance
- Has a clear vision/strategy for the team
- Has key technical skills to help advise the team
- Collaborates across Google
- Is a strong decision maker
其中只有一项直接提到了技术能力,对小团队来说,一个好的 Tech Lead 已经足够“管理”好团队了,但当团队规模变大后(可能只是10个人),继续微观管理反而会降低团队的效率,管理的目的从专注个人能力转变为放大团队。以一年 250 个工作日为例,一个人每年有大约 2000 个工作小时,那么 10 人团队一年就是 20000 小时,放大团队的能力将显得更为重要。
Tip
学习了在 Node.js 动态加载模块的方法,并用这个方法实现了基于 GitHub 的动态事件订阅和处理:
import { resolve, parse } from 'path'
import fg from 'fast-glob'
// 动态注册事件处理器
fg.sync('*.js', { cwd: resolve(__dirname, "events"), absolute: true })
.forEach((file: string) => {
const eventName = parse(file).name
app.on(eventName, async (context) => {
const eventHandler = require(file)
eventHandler.default(context)
})
});
Share
圣诞节放空一下,分享一段在终端飘雪花的脚本吧:
ruby -e 'C=`stty size`.scan(/\d+/)[1].to_i;S=["2743".to_i(16)].pack("U*");a={};puts "\033[2J";loop{a[rand(C)]=0;a.each{|x,o|;a[x]+=1;print "\033[#{o};#{x}H \033[#{a[x]};#{x}H#{S} \033[0;0H"};$stdout.flush;sleep 0.1}'