934 Shortest Bridge

Share

link: Shortest Bridge

Thought

Since the question ask for shortest, so BFS probabely the best algorithms to solve it. But notice, I will use 2 bfs funciton, one to find the edge of first island, the other one is to reach another island based on first edge.

BFS1:

input: start position
stop condition: queue empty
generate & expansion rule: find all its neighbors, mark as visited, and put '1' to queue -> will put that node in queue for further processing.
put '0' to result queue ->thats what we want to return!

BFS2

input: edge of first island
stop condition: reach any grid with number '1' -> find second island
generate & expansion rule: for any node in que, expend its unvisited neighbors in queue. And also record the level number.

Code

class Solution:
    def shortestBridge(self, A):
        """
        :type A: List[List[int]]
        :rtype: int
        """
        delta = ((0,1), (1,0), (0,-1), (-1,0))
        N = len(A)
        visited = set()
        def bfs1(x,y):
            # get the edge of first one
            visited.add((x,y))
            que = collections.deque()
            que.append((x,y))
            result = collections.deque()
            while que:
                c_x,c_y = que.popleft()
                if A[c_x][c_y] == 0:
                    result.append((c_x,c_y))
                    continue
                for dx,dy in delta:
                    nx,ny = dx+c_x, dy+c_y
                    if 0 <= nx <N and 0 <= ny < N and (nx,ny) not in visited:
                        que.append((nx,ny))
                        visited.add((nx,ny))
            return result
        def bfs2(starts):
            cnt = 0
            while starts:
                siz = len(starts)
                for i in range(siz):
                    c_x,c_y = starts.popleft()
                    if A[c_x][c_y] == 1:
                        return cnt
                    for dx,dy in delta:
                        nx,ny = dx+c_x, dy+c_y
                        if 0 <= nx <N and 0 <= ny < N and (nx,ny) not in visited:
                            starts.append((nx,ny))
                            visited.add((nx,ny))
                cnt += 1
        for i in range(N):
            for j in range(N):
                if A[i][j] == 1: # find start position 
                    t = bfs1(i,j) # get the edge
                    return bfs2(t) # get level number

Runtime

Time: O(n^2)
Space: O(n^2)

Read more

当代码不再稀缺 - 06

Prompt 不是新的 Source Code A prompt can generate an answer. It cannot, by itself, preserve why the answer should be true. 提示词可以生成一个回答,但它自己并不能证明这个回答为什么是对的 AI coding tools 刚开始流行时,有一个说法很有吸引力: Prompt 是新的 source code。 听起来很合理。 以前我们写 Python、Java、TypeScript;以后我们写自然语言,让模型完成 implementation。既然 prompt 决定 output,那就像管理代码一样把 prompt 保存、version、review,不就行了吗?

By andy
当代码不再稀缺 - 05

当代码不再稀缺 - 05

Code review 没有过时,但我们可能 review 错了对象 AI can produce a diff. Review decides whether the team is willing to own the change. AI会产出代码,但是团队应该决定是否拥有这个改变 上一篇:AI 时代还需要 Estimate 吗? 床位系统那个 bug 暴露以后,我反复想过一个问题: 我们明明做了 code review,为什么还是没有发现这个bug? 实现并不离谱。代码在判断查询时刻是否落在预约区间内,局部逻辑说得通,边界也长得像正常的时间处理。 只是业务真正问的是“这一天是否已经被占用”,代码回答的却是“当前这一秒是否落在区间”。 我们不是没看代码。我们看了一个错误问题的正确答案。 这件事让我意识到,AI 时代关于

By andy
当代码不再稀缺 - 04

当代码不再稀缺 - 04

AI 时代还需要 Estimate 吗? Don't estimate how long it takes to generate code. Estimate what it takes to trust the change. 不要只估算生成代码需要多久,要估算团队需要付出什么,才能相信这个 change。 上一篇:别再用更多 Ticket 衡量 AI Productivity 下一篇:Code review 没有过时,但我们可能 review 错了对象 如果现在有人问 engineer:“这个 ticket 要多久?”答案可能越来越像这样: “代码今天能出来。至于什么时候敢上线,我不知道。” 这不是

By andy
当代码不再稀缺 - 03

当代码不再稀缺 - 03

别再用更多 Ticket 衡量 AI Productivity Code is cheap. Verified outcomes are not. AI 可以廉价制造代码,但不能廉价制造可信结果。 上一篇:AI 没有消灭瓶颈:Slop 正在吞掉团队的注意力 下一篇:AI 时代还需要 Estimate 吗? 有一种项目周会,特别容易让 EM 心情愉快。 这个 sprint 完成的 ticket 比以前多了,PR 数量涨了,commit 也很活跃。自从团队开始用 AI,dashboard 上的每一条线都在往右上角走。管理层一看:不错,AI productivity 已经兑现了。 然后 reviewer 默默打开

By andy