Skip to content

Commit

Permalink
feat: 增加python 代码
Browse files Browse the repository at this point in the history
  • Loading branch information
azl397985856 authored May 8, 2020
1 parent 85d67d1 commit 032d353
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions problems/221.maximal-square.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,31 @@ dp[i - 1][j - 1]我们直接拿到,关键是`往上和往左进行延伸`, 最

## 代码

```js
代码支持:Python,JavaScript:

Python Code:

```python
class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
res = 0
m = len(matrix)
if m == 0:
return 0
n = len(matrix[0])
dp = [[0] * (n + 1) for _ in range(m + 1)]

for i in range(1, m + 1):
for j in range(1, n + 1):
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1 if matrix[i - 1][j - 1] == "1" else 0
res = max(res, dp[i][j])
return res ** 2
```


JavaScript Code:

```js

/*
* @lc app=leetcode id=221 lang=javascript
Expand Down Expand Up @@ -98,7 +120,8 @@ var maximalSquare = function(matrix) {
};
```


***复杂度分析***
- 时间复杂度:$O(M * N)$,其中M为行数,N为列数。
- 空间复杂度:$O(N)$,其中N为列数。

- 时间复杂度:$O(M * N)$,其中M为行数,N为列数。
- 空间复杂度:$O(M * N)$,其中M为行数,N为列数。

0 comments on commit 032d353

Please sign in to comment.