力扣 130. 被围绕的区域 python AC

并查集

class Solution:
    def solve(self, board):
        row, col = len(board), len(board[0])
        p = row * col
        bcj = [i for i in range(p + 1)]

        def find(x):
            if x != bcj[x]:
                bcj[x] = find(bcj[x])
            return bcj[x]

        def union(x, y):
            a, b = find(x), find(y)
            if a != b:
                bcj[a] = b

        for i in range(row):
            for j in range(col):
                if board[i][j] == 'O':
                    if i == 0 or i == row - 1 or j == 0 or j == col - 1:
                        union(i * col + j, p)
                    else:
                        for k in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
                            ii, jj = i + k[0], j + k[1]
                            if board[ii][jj] == 'O':
                                union(i * col + j, ii * col + jj)
        for i in range(row):
            for j in range(col):
                if find(p) != find(i * col + j):
                    board[i][j] = 'X'

dfs

class Solution:
    def solve(self, board):
        row, col = len(board), len(board[0])
        start = [(i, j) for i in range(row) for j in range(col) if board[i][j] == 'O' and (i == 0 or i == row - 1 or j == 0 or j == col - 1)]
        o = start.copy()

        def dfs(a, b):
            for x, y in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
                aa, bb = a + x, b + y
                if 0 <= aa < row and 0 <= bb < col and board[aa][bb] == 'O' and (aa, bb) not in o:
                    o.append((aa, bb))
                    dfs(aa, bb)

        for i, j in start:
            dfs(i, j)
        for i in range(row):
            for j in range(col):
                if (i, j) not in o:
                    board[i][j] = 'X'

### 力扣435题无重叠区间问题的Python解决方案 对于力扣435题——Non-overlapping Intervals,目标是从给定的一组区间中移除尽可能少的数量使得剩余区间互不重叠。一种高效的方法是采用贪心算法来解决这个问题。 为了有效地解决问题,可以先按照区间的结束位置升序排列这些区间[^1]。通过这种方式,在遍历时总是优先保留那些较早结束的区间,从而为后续可能加入更多非重叠区间留出空间。 下面是具体的Python实现代码: ```python class Interval(object): def __init__(self, start, end): self.start = start self.end = end def eraseOverlapIntervals(intervals): if not intervals: return 0 # Sort the intervals based on their ending time. sorted_intervals = sorted(intervals, key=lambda interval: interval.end) count = 0 prev_end = float('-inf') for current_interval in sorted_intervals: if current_interval.start >= prev_end: # If there is no overlap with previous selected interval, # update `prev_end` and continue without incrementing removals counter (`count`) prev_end = current_interval.end else: # Otherwise, this means we have an overlapping pair; # since intervals are already ordered by 'end', simply increase our removal tally (`count`) count += 1 return count ``` 此方法的时间复杂度主要取决于排序操作O(n log n),其中n表示输入区间的数量;而随后的一次线性扫描只需要O(n)时间。因此整体性能表现良好,适用于大多数实际应用场景中的数据规模[^2]。 值得注意的是,上述逻辑确保了每次遇到新的候选区间时都会选择最早结束的那个作为下一个非重叠部分的一部分,这样就能最大限度地减少被删除掉的区间数目[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值