947 Most Stones Removed with Same Row or Column

Share

Link: Most Stones Removed with Same Row or Column

Thought

When I saw the question, I realized its a Union Find question - For any union, I can keep only one stone form it. Then it convert to a count how many unique union finds exist question. 

So, only need to count all unique union find, and use all nodes to minus it. 

Code

class Solution:
    def removeStones(self, stones):
        """
        :type stones: List[List[int]]
        :rtype: int
        """
        N = len(stones)
        nodes = dict()
        cols = collections.defaultdict(set)
        rows = collections.defaultdict(set)
        counter = set()
        for x, y in stones: # pre process
            nodes[(x,y)] = (x,y)
            cols[x].add(y)
            rows[y].add(x)
        def find(x,y):
            if nodes[(x,y)]!= (x,y):
                nx,ny = find(*nodes[(x,y)])
                nodes[(x,y)] = (nx,ny)
            return nodes[(x,y)]
        def union(x,y,x2,y2):
            n_x , n_y = find(x,y)
            nn_x, nn_y = find(x2,y2)
            nodes[(nn_x,nn_y)] = (n_x, n_y)
        for x, y in stones:
            for y2 in cols[x]:
                union(x,y,x,y2)
            for x2 in rows[y]:
                union(x,y, x2,y)
        maxx = 0
        for x,y in stones:
            r_x, r_y = find(x,y)
            counter.add((r_x,r_y))
        return N - len(counter) 

Runtime

Time: O(n^2)

Space: O(n)

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