๐Ÿงฑ ์ดˆ๋ณด์ž์šฉ ๊ฐ€์ด๋“œ 'Maximal Rectangle' โ€“ LeetCode 85 (C++, Python, JavaScript)

๋ฐœํ–‰: (2026๋…„ 1์›” 11์ผ ์˜คํ›„ 02:18 GMT+9)
7 min read
์›๋ฌธ: Dev.to

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'๋งŒ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฐ€์žฅ ํฐ ์ง์‚ฌ๊ฐํ˜•์„ ์ฐพ์•„ ๊ทธ ๋ฉด์ ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์˜ˆ์‹œ

Example

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'๋“ค์˜ ๋†’์ด๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
๊ฐ ํ–‰์€ ํžˆ์Šคํ† ๊ทธ๋žจ์ด ๋œ๋‹ค.

ํžˆ์Šคํ† ๊ทธ๋žจ์—์„œ ๊ฐ€์žฅ ํฐ ์ง์‚ฌ๊ฐํ˜•์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค๋ฉด, ์ด ๊ณผ์ •์„ ๋ชจ๋“  ํ–‰์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ๋‹ค.
๋‹จ์กฐ ์Šคํƒ์„ ์‚ฌ์šฉํ•˜๋ฉด ์„ ํ˜• ์‹œ๊ฐ„์— ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค:

TermMeaning
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 ๋ Œ๋”๋ง โ€“ ๋ ˆ์ด์•„์›ƒ ์—”์ง„์„ ์œ„ํ•ด.

๋‹จ์กฐ ์Šคํƒ์„ ๋งˆ์Šคํ„ฐํ•˜๋ฉด ํ›จ์”ฌ ๋” ๊ฐ•๋ ฅํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ์‚ฌ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Back to Blog

๊ด€๋ จ ๊ธ€

๋” ๋ณด๊ธฐ ยป

๐ŸŽฏ ์ดˆ๋ณด์ž์šฉ ๊ฐ€์ด๋“œ 'N-Repeated Element in Size 2N Array' โ€“ LeetCode 961 (C++ | Python | JavaScript)

๋ฌธ์ œ ์„ค๋ช… ๊ธธ์ด๊ฐ€ 2N์ธ ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ฐฐ์—ด์—๋Š” N + 1๊ฐœ์˜ ๊ณ ์œ ํ•œ ์š”์†Œ๊ฐ€ ์žˆ์œผ๋ฉฐ, ๊ทธ ์ค‘ ์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ์š”์†Œ๊ฐ€ N๋ฒˆ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ๋ชฉํ‘œ...

๐Ÿงฑ ์ดˆ๋ณด์ž์šฉ ๊ฐ€์ด๋“œ โ€˜๊ทธ๋ฆฌ๋“œ์—์„œ ์ •์‚ฌ๊ฐํ˜• ๊ตฌ๋ฉ์˜ ๋ฉด์  ์ตœ๋Œ€ํ™”โ€™ โ€“ LeetCode 2943 (C++, Python, JavaScript)

Problem Overview ๋‹น์‹ ์—๊ฒŒ ์ฃผ์–ด์ง„ ๊ฒƒ์€: - ๊ฒฉ์ž(grid) ๋‚ด์˜ ๋‚ด๋ถ€ ์ˆ˜ํ‰ ๋ฐ ์ˆ˜์ง ๋ง‰๋Œ€์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ •์ˆ˜ n๊ณผ m. - ๋‘ ๋ฐฐ์—ด arrays, hBars์™€ vBars, listingโ€ฆ

๐ŸŒ ์ดˆ๋ณด์ž ์นœํ™” ๊ฐ€์ด๋“œ โ€˜๊ฐ€์žฅ ๊นŠ์€ ๋…ธ๋“œ๋“ค์„ ๋ชจ๋‘ ํฌํ•จํ•˜๋Š” ๊ฐ€์žฅ ์ž‘์€ ์„œ๋ธŒํŠธ๋ฆฌโ€™ โ€“ LeetCode 865 (C++, Python, JavaScript)

!ํ‘œ์ง€ ์ด๋ฏธ์ง€: ๐ŸŒ ์ดˆ๋ณด์ž ์นœํ™” ๊ฐ€์ด๋“œ โ€˜Smallest Subtree with all the Deepest Nodesโ€™ โ€“ LeetCode 865 C++, Python, JavaScript https://media2.dev.to/dynamic/ima