-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_catch(test_maximal_rectangle test.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include <stack> | ||
#include <vector> | ||
|
||
class Solution { | ||
public: | ||
static int maximalRectangle(const std::vector<std::vector<char>> &matrix) { | ||
const int n = matrix.size(), m = matrix.back().size(); | ||
|
||
int max_rectangle = 0; | ||
std::vector<int> heights(m); | ||
for (int i = 0; i < n; ++i) { | ||
for (int j = 0; j < m; ++j) { | ||
heights[j] = matrix[i][j] == '0' ? 0 : heights[j] + 1; | ||
} | ||
max_rectangle = std::max(max_rectangle, largestRectangleArea(heights)); | ||
} | ||
return max_rectangle; | ||
} | ||
|
||
private: | ||
static int largestRectangleArea(const std::vector<int> &heights) { | ||
const int n = heights.size(); | ||
std::stack<int> stack; | ||
int max_area = 0; | ||
for (int i = 0; i <= n; ++i) { | ||
while (!stack.empty() && (i == n || heights[stack.top()] > heights[i])) { | ||
const auto height = heights[stack.top()]; | ||
stack.pop(); | ||
const auto width = stack.empty() ? i : i - stack.top() - 1; | ||
max_area = std::max(max_area, height * width); | ||
} | ||
stack.push(i); | ||
} | ||
return max_area; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
std::vector<std::vector<char>> matrix; | ||
int expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.matrix{{'1', '0', '1', '0', '0'}, | ||
{'1', '0', '1', '1', '1'}, | ||
{'1', '1', '1', '1', '1'}, | ||
{'1', '0', '0', '1', '0'}}, | ||
.expected = 6, | ||
}, | ||
{ | ||
.matrix{{'0'}}, | ||
.expected = 0, | ||
}, | ||
{ | ||
.matrix{{'1'}}, | ||
.expected = 1, | ||
}, | ||
}; | ||
|
||
for (const auto &[matrix, expected] : test_cases) { | ||
const auto actual = Solution::maximalRectangle(matrix); | ||
REQUIRE(expected == actual); | ||
} | ||
} |