Algorithm
本周选择的算法题是:Unique Paths III。
规则
You are given an m x n
integer array grid
where grid[i][j]
could be:
1
representing the starting square. There is exactly one starting square.2
representing the ending square. There is exactly one ending square.0
representing empty squares we can walk over.-1
representing obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: grid = [[0,1],[2,0]]
Output: 0
Explanation: There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 20
1 <= m * n <= 20
-1 <= grid[i][j] <= 2
- There is exactly one starting cell and one ending cell.
Solution
class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
self.ans = 0
empty = 1
m, n = len(grid), len(grid[0])
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
x, y = i, j
elif grid[i][j] == 0:
empty += 1
def dfs(x, y, empty):
if not(0 <= x < m and 0 <= y < n and grid[x][y] != -1):
return
if grid[x][y] == 2:
if empty == 0:
self.ans += 1
return
grid[x][y] = -1
dfs(x + 1, y, empty - 1)
dfs(x - 1, y, empty - 1)
dfs(x, y + 1, empty - 1)
dfs(x, y - 1, empty - 1)
grid[x][y] = 0
dfs(x, y, empty)
return self.ans
Review
Web 3.0 Will Nuke Social Media As We Know It
挺有趣的观点,作者希望我们能在 Web 3.0 时代完全拥有自己的数据,能决定自己的数据要不要交给别人(比如付费),或者被用于什么地方。
其实我们曾经在 Web 1.0 时代就是这样的,用户有自己的页面,页面与页面之间用友链的形式关联,不那么依赖 Google;其次那时候主流的玩法就是去中心化,比如邮件,服务的提供方与使用方只有采用开放的 SMTP、POP 等协议,可以随意选择自己期望的客户端和服务端程序,没有其他的限制;不同的服务器之间也有 XMPP、Web Service 这样开放的通信协议,似乎用户的数据并没有被采集起来。
然而 1.0 时代还是过去了,大家越来越享受用隐私换取的便利性,不过在未来,用户或许能要求数据的采集者提供更多、更明确的“价值”,由用户根据这些潜在的“价值”选择要不要交出自己的数据,没有价值则没有数据。
Tip
在 WKWebView 中使用自定义字体的方法:How to use custom fonts in WKWebView。
Share
UX 领域有个 4D 方法论:
- Discover — understanding the problem(s) and developing insights
- Design — the area to focus upon
- Develop — potential solutions to the problem(s)
- Deliver — solutions that work
这个也能用于描述中台职责:
发现问题(Discover):在有具体的动作前,一定要建立全局视野,围绕公司业务、行业趋势做深入的分析,一定要确保站在全局视角
业务梳理(Define):经过全局分析与思考后,接下来就要对公司业务进行梳理,提炼出真正要做的是什么,以及先做、后做什么等
设计方案(Develop):明白我们要做什么之后,立项,当作一个企业产品来做,从需求分析开始,按照产品生产的标准实践、开发
交付落地(Deliver):交付过程中,可能会发现很多新问题或者在实际应用之后产生的新问题,这个时候就要快速响应迭代
毫无疑问,客户满意度是交付的重要衡量指标,中台的用户群体是谁?那就是整个公司的业务线和用户,不是某条线的特定需求,也不是某些短期需求,中台考虑的一定要是长期对公司所产生的价值。