Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the maximum Square by LeetCode

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to achieve the largest square in LeetCode. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

one

Topic description

0 and 1 form a character two-dimensional matrix in which the largest square containing only 1 is found and its area is returned. Such as: input

[["1", "0", "1", "0", "0"]

["1", "0", "1", "1"]

["1", "1", "1", "1"]

["1", "0", "0", "1", "0"]

Return 4.

two

Answer to the question

Idea: dynamic programming in LeetCode exercise DAY 2: longest reply substring we introduce the meaning of dynamic programming, this time we will not repeat it, but go directly to the logic.

The first step is to find the intermediate state: here the intermediate state st [I] [j] means that the maximum square side length can be obtained by using the (iMagnej) element in the matrix as the vertex in the lower-right corner of the square.

The second step is to determine the state transition: if (iMagnej) is 0, the current position state value is 0, otherwise the state value depends on the upper, left and upper left corner state values, and the transfer relationship is st [I] [j] = min (st [I-1] [j] = min (st [I-1] [j], st [I] [JF1]) + 1, that is, the surrounding minimum state value + 1. If you don't understand, you can find a case and calculate it manually. It is also important to note that if the element is at the outermost of the entire matrix, the state value can be judged only by the value of the element.

Class Solution: def maximalSquare (self Matrix: list [list [str])-> int: if len (matrix) = = 0: return 0 # len (matrix) is the number of matrix rows st = [[0] * len (matrix [0]) for _ in range (len (matrix))] maxlong = [] for i in range (len (matrix)): for j in range (matrix [0]) ): if matrix [I] [j] = '1regions: if i*j==0: st [I] [j] = 1 else: st [I] [j] = min (st [I-1] [j] St [I-1] [Jmur1], st [I] [jmur1]) + 1 maxlong.append (max (STI)) return max (maxlong) * * 2

This is the end of the article on "how to achieve the largest square in LeetCode". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report