LeetCode Online Judge 题目C# 练习 - Maximal Rectangle
2012-09-25 05:30
423 查看
Given a 2D matrix fill with 0's and 1's, find the largest rectangle containing all ones and return its area.
代码分析:
Brute Force, O(n4). 每一个是1的格子,都从上至下,从左往右,去看最大面积。lastj变量是存放最小的列。
例如:
[0,0] : 第一行: curr_area: 2, max_area: 2, lastj = 2
第二行: curr_area: 2, max_area: 2, lastj = 1
第三行: curr_area: 3, max_area: 3, lastj = 1
max_area = 3;
...
代码分析:
利用DP,第一次n2循环,先预处理。然后再O(n3)计算最大面积。
例如:
DP处理后生成新的2D- array
每个格子相应的最大面积
public static int MaximalRectangle(List<List<char>> matrix) { int n = matrix.Count; int m = matrix[0].Count; int curr_area = 0; int max_area = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if(matrix[i][j] == '1') { int lastj = m; for (int k = i; k < n && matrix[k][j] == '1'; k++) { for (int l = j; l < lastj; l++) { if (matrix[k][l] == '1') { curr_area = (l - j + 1) * (k - i + 1); max_area = Math.Max(max_area, curr_area); } else lastj = l; } } } } } return max_area; }
代码分析:
Brute Force, O(n4). 每一个是1的格子,都从上至下,从左往右,去看最大面积。lastj变量是存放最小的列。
例如:
1 | 1 | 0 | 1 |
1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 |
0 | 0 | 1 | 1 |
第二行: curr_area: 2, max_area: 2, lastj = 1
第三行: curr_area: 3, max_area: 3, lastj = 1
max_area = 3;
...
public static int MaximalRectangleOpt(List<List<char>> matrix) { int n = matrix.Count; int m = matrix[0].Count; int[,] dp = new int[n, m]; int curr_area = 0; int max_area = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (matrix[i][j] == '1') dp[i, j] = j > 0 ? dp[i, j - 1] + 1 : 1; else dp[i, j] = 0; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int k = i; int min = dp[i, j]; while (k >= 0 && dp[k,j] > 0) { min = Math.Min(min, dp[k, j]); curr_area = min * (i - k + 1); max_area = Math.Max(max_area, curr_area); k--; } } } return max_area; }
代码分析:
利用DP,第一次n2循环,先预处理。然后再O(n3)计算最大面积。
例如:
1 | 1 | 0 | 1 |
1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 |
0 | 0 | 1 | 1 |
1 | 2 | 0 | 1 |
1 | 0 | 1 | 2 |
1 | 2 | 3 | 4 |
0 | 0 | 1 | 2 |
1 | 2 | 0 | 1 |
2 | 0 | 1 | 2 |
3 | 2 | 3 | 4 |
0 | 0 | 3 | 6 |
相关文章推荐
- LeetCode Online Judge 题目C# 练习 - Maximum Subarray
- LeetCode Online Judge 题目C# 练习 - Unique Paths
- LeetCode Online Judge 题目C# 练习 - Divid Two Integer
- LeetCode Online Judge 题目C# 练习 - Climbing Stairs
- LeetCode Online Judge 题目C# 练习 - Merge Sorted Array
- LeetCode Online Judge 题目C# 练习 - Anagrams
- LeetCode Online Judge 题目C# 练习 - Same Tree
- LeetCode Online Judge 题目C# 练习 - String to Integer (atoi)
- LeetCode Online Judge 题目C# 练习 - Valid Number
- LeetCode Online Judge 题目C# 练习 - Regular Expression Matching
- LeetCode Online Judge 题目C# 练习 - Sprial Matrix
- LeetCode Online Judge 题目C# 练习 - 3SUM Closest
- LeetCode Online Judge 题目C# 练习 - Subsets
- LeetCode Online Judge 题目C# 练习 - Valid Sudoku
- LeetCode Online Judge 题目C# 练习 - Combination Sum
- LeetCode Online Judge 题目C# 练习 - Subsets II
- LeetCode Online Judge 题目C# 练习 - Word Search
- LeetCode Online Judge 题目C# 练习 - Binary Tree Inorder Traversal
- LeetCode Online Judge 题目C# 练习 - Edit Distance
- LeetCode Online Judge 题目C# 练习 - ZigZag Conversion