Neterukun's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Neterukun1993/Library

:heavy_check_mark: ヒストグラム中の最大長方形
(DP/largest_rectangle_in_histogram.py)

概要

ヒストグラム中の最大長方形の面積を求めるアルゴリズム。スタックを用いた方法により、ビンの数の線形時間で計算できる。

使い方

largest_rectangle_in_histogram(heights: Sequence[int]) -> int
$N$ 個のビン heights を持つヒストグラム中の最大長方形の面積を返す。計算量 $O(N)$

Required by

Verified with

Code

def largest_rectangle_in_histogram(heights):
    heights = heights + [-1]  # sentinel
    ans = 0
    stack = []
    for i, height in enumerate(heights):
        idx = i
        while True:
            if not stack or stack[-1][0] <= height:
                stack.append((height, idx))
                break
            else:
                h, l = stack.pop()
                ans = max(h * (i - l), ans)
                idx = l
    return ans
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/onlinejudge_verify/documentation/build.py", line 71, in _render_source_code_stat
    bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/onlinejudge_verify/languages/python.py", line 96, in bundle
    raise NotImplementedError
NotImplementedError
Back to top page