๐งฑ ์ด๋ณด์์ฉ ๊ฐ์ด๋ 'Maximal Rectangle' โ LeetCode 85 (C++, Python, JavaScript)
Iโm happy to help translate the article, but it looks like the body of the text isnโt included in your messageโonly the source link is present. Could you please provide the articleโs content (or the specific sections youโd like translated)? Once I have the text, Iโll translate it into Korean while preserving the original formatting and code blocks.
๋ฌธ์ ์์ฝ
์ฃผ์ด์ง ๊ฒ
'0'์'1'๋ฌธ์๋ก ์ฑ์์ง 2โD ์ด์งmatrix.
๋ชฉํ
'1'๋ง์ผ๋ก ์ด๋ฃจ์ด์ง ๊ฐ์ฅ ํฐ ์ง์ฌ๊ฐํ์ ์ฐพ์ ๊ทธ ๋ฉด์ ์ ๋ฐํํ๋ค.
์์
Input: matrix = [
["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]
]
Output: 6
Explanation: The maximal rectangle is shown in the picture above.
์ค๋ช : ์ ๊ทธ๋ฆผ์ ์ต๋ ์ง์ฌ๊ฐํ์ด ํ์๋์ด ์์ต๋๋ค.
์ง๊ด
๋ชจ๋ ๊ฐ๋ฅํ ์ง์ฌ๊ฐํ์ ์ง์ ํ์ธํ๋ ๊ฒ์ ํ์ค์ ์ด์ง ์๋ค.
๋์ ๊ฐ ํ์ ๋ฐ๋ฅ์ผ๋ก ๊ฐ์ฃผํ๊ณ ๊ทธ ์์ ์์ธ ์ฐ์๋ '1'๋ค์ ๋์ด๋ฅผ ๊ณ์ฐํ๋ค.
๊ฐ ํ์ ํ์คํ ๊ทธ๋จ์ด ๋๋ค.
ํ์คํ ๊ทธ๋จ์์ ๊ฐ์ฅ ํฐ ์ง์ฌ๊ฐํ์ ์ฐพ์ ์ ์๋ค๋ฉด, ์ด ๊ณผ์ ์ ๋ชจ๋ ํ์ ๋ํด ๋ฐ๋ณตํ ์ ์๋ค.
๋จ์กฐ ์คํ์ ์ฌ์ฉํ๋ฉด ์ ํ ์๊ฐ์ ํ์ํ ์ ๋ณด๋ฅผ ์ป์ ์ ์๋ค:
| Term | Meaning |
|---|---|
| NSE (Next Smaller Element) | ํ์ฌ ๋ฐ๋ณด๋ค ์ค๋ฅธ์ชฝ์ ์๋ ์ฒซ ๋ฒ์งธ ๋ ์์ ๋ฐ |
| PSE (Previous Smaller Element) | ํ์ฌ ๋ฐ๋ณด๋ค ์ผ์ชฝ์ ์๋ ์ฒซ ๋ฒ์งธ ๋ ์์ ๋ฐ |
์ธ๋ฑ์ค i์ ์๋ ๋ฐ์ ๋ํด:
width = NSE[i] - PSE[i] - 1
area = heights[i] * width
๋ชจ๋ ํ์ ๋ํ ์ต๋ ๋ฉด์ ์ด ๋ต์ด๋ค.
์ฝ๋ ๋ธ๋ก
C++ ์๋ฃจ์
class Solution {
public:
int maximalRectangle(vector>& matrix) {
if (matrix.empty()) return 0;
int n = matrix.size(), m = matrix[0].size();
vector heights(m, 0), pse(m), nse(m);
stack ps, ns;
int max_area = 0;
for (int i = 0; i heights[j]) {
nse[ns.top()] = j;
ns.pop();
}
ns.push(j);
while (!ps.empty() && heights[ps.top()] >= heights[j]) {
ps.pop();
}
pse[j] = (ps.empty() ? -1 : ps.top());
ps.push(j);
}
// Update max area for this histogram
for (int j = 0; j int:
if not matrix:
return 0
Python (๋ถ๋ถ ์ค๋ํซ)
int:
if not matrix:
return 0
Python ์๋ฃจ์
def maximalRectangle(matrix):
if not matrix:
return 0
rows, cols = len(matrix), len(matrix[0])
heights = [0] * cols
max_area = 0
for i in range(rows):
# Update heights based on the current row
for j in range(cols):
heights[j] = heights[j] + 1 if matrix[i][j] == '1' else 0
# Largest Rectangle in Histogram for this row
stack = []
pse = [-1] * cols
nse = [cols] * cols
# Next Smaller Element
for j in range(cols):
while stack and heights[stack[-1]] > heights[j]:
nse[stack.pop()] = j
stack.append(j)
# Previous Smaller Element (backwards pass)
stack.clear()
for j in range(cols - 1, -1, -1):
while stack and heights[stack[-1]] > heights[j]:
pse[stack.pop()] = j
stack.append(j)
# Compute area for each bar
for j in range(cols):
area = heights[j] * (nse[j] - pse[j] - 1)
max_area = max(max_area, area)
return max_area
JavaScript ์๋ฃจ์ (๋ฒ์ โฏ1)
/**
* @param {character[][]} matrix
* @return {number}
*/
var maximalRectangle = function(matrix) {
if (matrix.length === 0) return 0;
const rows = matrix.length;
const cols = matrix[0].length;
const heights = new Array(cols).fill(0);
let maxArea = 0;
for (let i = 0; i heights[j]) {
nse[stack.pop()] = j;
}
stack.push(j);
}
// Previous Smaller Element (reverse pass)
stack.length = 0; // clear stack
for (let j = cols - 1; j >= 0; --j) {
while (stack.length && heights[stack[stack.length - 1]] > heights[j]) {
pse[stack.pop()] = j;
}
stack.push(j);
}
// Compute area for each bar
for (let j = 0; j heights[j]) {
nse[stack.pop()] = j;
}
stack.push(j);
}
// Reset stack for PSE
stack.length = 0;
for (let j = cols - 1; j >= 0; j--) {
while (stack.length && heights[stack[stack.length - 1]] > heights[j]) {
pse[stack.pop()] = j;
}
stack.push(j);
}
// Compute max area for this row
for (let j = 0; j < cols; j++) {
maxArea = Math.max(maxArea, heights[j] * (nse[j] - pse[j] - 1));
}
}
return maxArea;
}
JavaScript ์๋ฃจ์ (๋ฒ์ โฏ2)
/**
* @param {character[][]} matrix
* @return {number}
*/
var maximalRectangle = function(matrix) {
if (matrix.length === 0) return 0;
const rows = matrix.length;
const cols = matrix[0].length;
const heights = new Array(cols).fill(0);
let maxArea = 0;
for (let i = 0; i < rows; ++i) {
// Update heights
for (let j = 0; j < cols; ++j) {
heights[j] = matrix[i][j] === '1' ? heights[j] + 1 : 0;
}
// Compute NSE
const nse = new Array(cols).fill(cols);
const stack = [];
for (let j = 0; j < cols; ++j) {
while (stack.length && heights[stack[stack.length - 1]] > heights[j]) {
nse[stack.pop()] = j;
}
stack.push(j);
}
// Compute PSE
const pse = new Array(cols).fill(-1);
stack.length = 0;
for (let j = cols - 1; j >= 0; --j) {
while (stack.length && heights[stack[stack.length - 1]] > heights[j]) {
pse[stack.pop()] = j;
}
stack.push(j);
}
// Calculate max area for this row
for (let j = 0; j < cols; ++j) {
maxArea = Math.max(maxArea, heights[j] * (nse[j] - pse[j] - 1));
}
}
return maxArea;
}
ํต์ฌ ์์
- ๋ฌธ์ ์ถ์: 2โD ๋ฌธ์ ๋ฅผ 1โD ๋ฌธ์ (ํ์คํ ๊ทธ๋จ)์ ์ฐ์์ผ๋ก ๋ณด๋ ๊ฒ์ด ๊ฐ๋ ฅํ ์ฌ๊ณ ๋ชจ๋ธ์ด๋ค.
- ๋จ์กฐ ์คํ: ์ด๋ O(n) ์๊ฐ์ โ๊ฐ์ฅ ๊ฐ๊น์ด ์์โ ๋๋ โ๊ฐ์ฅ ๊ฐ๊น์ด ํฐโ ์์๋ฅผ ์ฐพ๋ ๋ฐ ํ์์ ์ด๋ค.
- ๊ณต๊ฐโ์๊ฐ ํธ๋ ์ด๋์คํ: ๋์ด์ ์คํ์ ์ํด O(n) ์ถ๊ฐ ๊ณต๊ฐ์ ์ฌ์ฉํจ์ผ๋ก์จ ์ ์ฒด ์๊ฐ ๋ณต์ก๋๋ฅผ O(mโฏรโฏn) ๋ก ๋ง๋ค ์ ์์ผ๋ฉฐ, ์ด๋ ์์ ํ์๋ณด๋ค ํจ์ฌ ๋น ๋ฅด๋ค.
์ต์ข ์๊ฐ
์ด ๋ฌธ์ ๋ ์ฌ๋ฌ ๋จ๊ณ์ ๋ ผ๋ฆฌ๋ฅผ ๊ฒฐํฉํ๊ธฐ ๋๋ฌธ์ ๊ณ ๊ธ ๊ธฐ์ ๋ฉด์ ์์ ํ์์ ์ธ ๋ฌธ์ ์ ๋๋ค. ์ค์ ์์ง๋์ด๋ง์์๋ ์ด๋ฌํ ๊ธฐ์ ์ด ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉ๋ฉ๋๋ค:
- ์ปดํจํฐ ๋น์ โ ๊ฐ์ฒด ํ์ง๋ฅผ ์ํด.
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ต์ ํ โ ์ฐ์๋ ์์ ๊ณต๊ฐ ๋ธ๋ก์ ์ฐพ๊ธฐ ์ํด.
- UI ๋ ๋๋ง โ ๋ ์ด์์ ์์ง์ ์ํด.
๋จ์กฐ ์คํ์ ๋ง์คํฐํ๋ฉด ํจ์ฌ ๋ ๊ฐ๋ ฅํ ๋ฌธ์ ํด๊ฒฐ์ฌ๊ฐ ๋ ์ ์์ต๋๋ค.
