1. 程式人生 > >Leetcode84. Largest Rectangle in Histogram

Leetcode84. Largest Rectangle in Histogram

  • 沒通過測試 有個一個0-20000的例子timeout
  • 思路 : 雙層迴圈 處理[i , j]之間的值, 其中儲存height在[i,j]最小值, 因為短板效應,最小的值決定水的高度
class Solution:
    def largestRectangleArea(self, heights):
        waters = 0
        res = 0
        if heights is None:
            return 0 
        if len(heights) == 1:
            return heights[0]
        for i in range(len(heights)):
            min_value = heights[i]
            waters = heights[i]
            for j in range(i, len(heights)):
                if i == j:
                    res = max(waters, res)
                else:
                    min_value = min(min_value, heights[j])
                    waters = min_value *(j-i+1)
                    res = max(waters, res)
        return res

相關推薦

(python stack) leetcode84 Largest Rectangle in Histogram

這道題使用的知識點是:棧 從左到右,如果當前對應的小矩形高度大於棧頂對應小矩形高度,進棧,移動到下一個小矩形,,,,,,,,, 否則棧頂元素出棧,再次比較此時棧頂元素和當前對應的小矩形高度,如果前者還是大於後者,繼續出棧,直到小於等於為止 依次計算上面一次連續出棧的

LeetCode84 Largest Rectangle in Histogram

Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the

Leetcode84. Largest Rectangle in Histogram

沒通過測試 有個一個0-20000的例子timeout 思路 : 雙層迴圈 處理[i , j]之間的值, 其中儲存height在[i,j]最小值, 因為短板效應,最小的值決定水的高度 class Solution: def largestRectan

【LeetCode】84. Largest Rectangle in Histogram——直方圖最大面積

the area 求解 技術分享 ges sent -s com col alt Given n non-negative integers representing the histogram‘s bar height where the width of each ba

84. Largest Rectangle in Histogram

rect turn height urn bsp str cnblogs emp ogr class Solution { public int largestRectangleArea(int[] heights) { Stack<I

leetcode 84. Largest Rectangle in Histogram

分享 說明 -i right -h set code tco his link Given n non-negative integers representing the histogram‘s bar height where the width of each b

【leetcode】84. Largest Rectangle in Histogram

class 左右 動態 eight span tco img tro list 題目如下: 解題思路:這個問題考的是木桶原理,所以我們的關註點就是找到最短的木板。假設Largest Rectangle 的區間是從heights[i-j],並且heights[k]是其中最短

Largest Rectangle in Histogram -- LeetCode

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

LeetCode - Container With Most Water、Largest Rectangle in Histogram - 直方圖中面積問題

本文會總結以下兩道題目: 11 - Container With Most Water - 直方圖中盛水問題 (Medium) 84 - Largest Rectangle in Histogram - 直方圖中最大矩形面積問題 (Hard) 還有兩道題目也是直方圖中

LeetCode:84. Largest Rectangle in Histogram

題目: Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectang

Largest Rectangle in Histogram 直方圖中最大的矩形

Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest re

Leetcode 84. Largest Rectangle in Histogram 柱狀圖的最大矩形面積

解決思路:        問題主要考察每一根柱子最大能擴多大,這個行為的實質就是找到柱子左邊剛比它小的柱子的位置在哪裡,以及右邊剛比它小的柱子位置在哪裡,為了模擬這個過程,我們可以用棧模擬這個過程。        棧裡邊儲存的是一個遞增柱子的位置,每次遇到一個比棧頂位

[LeetCode] Largest Rectangle in Histogram 直方圖中最大的矩形

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogr

LeetCode 筆記系列 17 Largest Rectangle in Histogram

題目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area o

Leetcode:84. Largest Rectangle in Histogram(面積最大的長方形)

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle i

Leetcode刷題記—— 84. Largest Rectangle in Histogram(柱形圖中最大矩形面積)

一、題目敘述: Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of large

【LeetCode】84. Largest Rectangle in Histogram(C++)

地址:https://leetcode.com/problems/largest-rectangle-in-histogram/ 題目: Given n

leetcode題解分析_84. Largest Rectangle in Histogram

【題目】 題目連結 Given n non-negative integers representing the histogram’s bar height where the width of

Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle

尋找直方圖中的最大矩形 Largest Rectangle in Histogram

題目:Given n non-negative integers representing the histogram's bar height where the width of each ba