網頁

2025年11月14日 星期五

Py3 C++ 2D sweep解Leetcode 2536 Increment Submatrices by One


Py3 C++ 2D sweep解Leetcode 2536  Increment Submatrices by One
使用2D掃描,方法為line sweep的2D推廣,方便好用。
[Py3解請進]

----
Using 2D scanning, specifically the 2D extension of line sweep, is convenient and easy to use.

class Solution:
    def rangeAddQueries(self, n: int, queries: List[List[int]]) -> List[List[int]]:
        arr=[[0]*n for _ in range(n+1)]
        for r1, c1, r2, c2 in queries:
            arr[r1][c1]+=1
            arr[r2+1][c1]-=1
            if c2+1<n:
                arr[r1][c2+1]-=1
                arr[r2+1][c2+1]+=1
        for i in range(n):
            arr[i]=list(accumulate(arr[i]))
        for j in range(n):
            for i in range(1, n):
                arr[i][j]+=arr[i-1][j]
        return arr[:n]
 

沒有留言:

張貼留言

HTML 編輯器