当前位置:   article > 正文

python写算法题:leetcode: 84. Largest Rectangle in Histogram_python 84. largest rectangle in histogram

python 84. largest rectangle in histogram
  1. class Solution(object):
  2. def _largestArea(self, sortHeight, ind, heights, left, right):
  3. if right<=left:
  4. return 0
  5. if right-left<=1:
  6. return heights[left]*(right-left)
  7. while ind<len(sortHeight) and not left<=sortHeight[ind][0]<right:
  8. ind+=1
  9. if ind>=len(sortHeight):
  10. return 0
  11. return max((right-left)*sortHeight[ind][1],
  12. self._largestArea(sortHeight, ind+1, heights, left, sortHeight[ind][0]),
  13. self._largestArea(sortHeight, ind+1, heights, sortHeight[ind][0]+1, right)
  14. )
  15. def largestRectangleArea(self, heights):
  16. """
  17. :type heights: List[int]
  18. :rtype: int
  19. """
  20. sortHeights = sorted([v for v in enumerate(heights)], key=lambda x:x[1])
  21. return self._largestArea(sortHeights, 0, heights, 0, len(sortHeights))

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/380495
推荐阅读
相关标签
  

闽ICP备14008679号