Skip to content

Commit 8fcdd2b

Browse files
committed
221_Maximal_Square
228_Summary_Ranges
1 parent 27e3ae2 commit 8fcdd2b

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,6 @@
6969
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/217_Contains_Duplicate.py) | 1. Set and compare length<br>2. Sort and check i,i +1|
7070
| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/219_Contains_Duplicate_II.py) | 1. Brute force<br> 2. Maintenance a set that contains previous k numbers, and check if curr in set |
7171
| 220 | [Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/220_Contains_Duplicate_III.py) | 1. Sort and binary Search <br>2. Bucket sort |
72+
| 221 | [Maximal Square](https://leetcode.com/problems/maximal-square/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/221_Maximal_Square.py) | 1. Brute force<br> 2. dp[i][j] = min(dp[i-1][j], dp[i-1][j-1], dp[i][j-1]) + 1, O(mn) and O(mn)<br>3. dp[j] = min([j], dp[j-1], prev) + 1, O(mn) and O(n)|
73+
| 228 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/228_Summary_Ranges.py) | Detect start and jump, O(n) and O(1) |
7274
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py) | Maintain a sliding window with at most k distinct characters and a count for this window. Note that the start position need a loop to update.|

python/221_Maximal_Square.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Solution(object):
2+
# def maximalSquare(self, matrix):
3+
# """
4+
# :type matrix: List[List[str]]
5+
# :rtype: int
6+
# """
7+
# # Brute force O(mn^2)
8+
# if matrix is None or len(matrix) == 0:
9+
# return 0
10+
# rows, cols = len(matrix), len(matrix[0])
11+
# res = 0
12+
# for i in range(rows):
13+
# for j in range(cols):
14+
# if matrix[i][j] == '1':
15+
# sqlen, flag = 1, True
16+
# while sqlen + i < rows and sqlen + j < cols and flag:
17+
# for k in range(j, sqlen + j + 1):
18+
# if matrix[i + sqlen][k] == '0':
19+
# flag = False
20+
# break
21+
# for k in range(i, sqlen + i + 1):
22+
# if matrix[k][j + sqlen] == '0':
23+
# flag = False
24+
# break
25+
# if flag:
26+
# sqlen += 1
27+
# if res < sqlen:
28+
# res = sqlen
29+
# return res * res
30+
31+
# def maximalSquare(self, matrix):
32+
# # dp[i][j] = min(dp[i-1][j],dp[i-1][j-1],dp[i][j-1])+1
33+
# if matrix is None or len(matrix) == 0:
34+
# return 0
35+
# rows, cols, res = len(matrix), len(matrix[0]), 0
36+
# dp = [[0] * (cols + 1) for _ in range(rows + 1)]
37+
# for i in range(1, rows + 1):
38+
# for j in range(1, cols + 1):
39+
# if matrix[i - 1][j - 1] == '1':
40+
# dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
41+
# res = max(res, dp[i][j])
42+
# return res * res
43+
44+
def maximalSquare(self, matrix):
45+
# dp[j] = min([j], dp[j-1], prev) + 1
46+
# O(n) space
47+
if matrix is None or len(matrix) == 0:
48+
return 0
49+
rows, cols, res, prev = len(matrix), len(matrix[0]), 0, 0
50+
dp = [0] * (cols + 1)
51+
for i in range(1, rows + 1):
52+
for j in range(1, cols + 1):
53+
temp = dp[j]
54+
if matrix[i - 1][j - 1] == '1':
55+
dp[j] = min(dp[j - 1], dp[j], prev) + 1
56+
res = max(res, dp[j])
57+
else:
58+
dp[j] = 0
59+
prev = temp
60+
return res * res
61+

python/228_Summary_Ranges.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution(object):
2+
# def summaryRanges(self, nums):
3+
# """
4+
# :type nums: List[int]
5+
# :rtype: List[str]
6+
# """
7+
# if nums is None or len(nums) == 0:
8+
# return []
9+
# res = []
10+
# start, prev, ls = nums[0], nums[0], len(nums)
11+
# for i in range(ls):
12+
# curr = nums[i]
13+
# if curr - prev > 1:
14+
# if start == prev:
15+
# res.append("%d" % start)
16+
# else:
17+
# res.append("%d->%d" % (start, prev))
18+
# start = curr
19+
# prev = curr
20+
# if start == prev:
21+
# res.append("%d" % start)
22+
# else:
23+
# res.append("%d->%d" % (start, prev))
24+
# return res
25+
26+
def summaryRanges(self, nums):
27+
res = []
28+
start, ls = 0, len(nums)
29+
for i in range(ls):
30+
if i + 1 < ls and nums[i + 1] == nums[i] + 1:
31+
continue
32+
if i == start:
33+
res.append(str(nums[start]))
34+
else:
35+
res.append("%d->%d" % (nums[start], nums[i]))
36+
start = i + 1
37+
return res

0 commit comments

Comments
 (0)