avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

1139. The greatest 1 Formation of the border One question daily

  2024-01-01        
字数统计: 331字   |   阅读时长: 2min

topic:

2023-02-17.png
1139. The greatest 1 Formation of the border.md

Thought:

quiz6 Simple version,Prefix and求解。It should be noted that the upper left corner is0 Special circumstances are not considered,
Need to verify # superior Left Down right Four -edge 1 The number of individuals d

Code:

错误Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution:
def largest1BorderedSquare(self, grid: List[List[int]]) -> int:
matrix_heng = copy.deepcopy(grid)
matrix_su = copy.deepcopy(grid)
ans = 0
if len(grid) == 1:
if 1 in grid[0]:
return 1
else:return 0
for ind, i in enumerate(grid):
for index in range(len(i)):
# Each line adds
if index >= 1:
matrix_heng[ind][index] += matrix_heng[ind][index-1]
# Add each column
if ind >= 1:
matrix_su[ind][index] += matrix_su[ind-1][index]
print(matrix_heng, matrix_su)
for i in range(len(grid)):
for j in range(len(grid[i])):
minus = min(matrix_heng[i][j], matrix_su[i][j])
try:
if grid[i-minus+1][j-minus+1] == 1 and i >= minus-1 and j >= minus-1:
ans = max(ans, minus ** 2)
except:
pass
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
def largest1BorderedSquare(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
rs = [list(accumulate(row, initial=0)) for row in grid] # 每行的Prefix and
cs = [list(accumulate(col, initial=0)) for col in zip(*grid)] # 每列的Prefix and
for d in range(min(m, n), 0, -1): # From large to small enumeration square edges d
for i in range(m - d + 1):
for j in range(n - d + 1): # 枚举正方形Leftsuperior角坐标 (i,j)
# superior Left Down right Four -edge 1 The number of individuals d
if rs[i][j + d] - rs[i][j] == d and \
cs[j][i + d] - cs[j][i] == d and \
rs[i + d - 1][j + d] - rs[i + d - 1][j] == d and \
cs[j + d - 1][i + d] - cs[j + d - 1][i] == d:
return d * d
return 0
  • Python
  • answer
  • Prefix and

扫一扫,分享到微信

微信分享二维码
1138. The path on the letter board One question daily
1234. Replace the sub -string to get a balanced string One question daily
目录
  1. 1. topic:
  2. 2. Thought:
  3. 3. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号