• 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 histogram.


    Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].


    The largest rectangle is shown in the shaded area, which has area = 10 unit.

    For example,
    Given height = [2,1,5,6,2,3],
    return 10

    解决思路

    巧妙的利用辅助栈。

    (1)栈内存储的是数组遍历过程中的非递减元素的下标;

    (2)如果遍历的当前元素比栈顶元素更小,则弹出栈顶值(下标)的索引元素,作为高度;宽度要看栈是否为空,如果为空则直接为下标值,否则为当前遍历的下标减去栈顶值再减1;

    (3)遍历结束,如果栈内还有元素,则继续计算。

    时间/空间复杂度均为O(n).

    程序

    public class Solution {
        public int largestRectangleArea(int[] height) {
            if (height == null || height.length == 0) {
                return 0;
            }
            
            Stack<Integer> s = new Stack<>();
            int max = 0;
            
            for (int i = 0; i <= height.length; i++) {
                int cur = i == height.length ? -1 : height[i]; // trick
                while (!s.isEmpty() && cur < height[s.peek()]) {
                    int h = height[s.pop()];
                    int w = s.isEmpty() ? i : i - s.peek() - 1;
                    max = Math.max(max, h * w);
                }
                s.push(i);
            }
            
            return max;
        }
    }
    

    Follow up

    Maximal Rectangle

    问题描述

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.

    解决思路

    假设矩阵的行数为n, 列数为m.

    首先将矩阵转化为n个rectangle,然后利用上面的方法计算max,最后输出这n个中的max即可。

    程序

    public class Solution {
        public int maximalRectangle(char[][] matrix) {
            if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
                return 0;
            }
            
            int[][] nums = transform(matrix);
            int max = 0;
            
            for (int i = 0; i < matrix.length; i++) {
                max = Math.max(max, getMaxRectangle(nums[i]));
            }
            
            return max;
        }
        
        private int getMaxRectangle(int[] nums) {
            if (nums == null || nums.length == 0) {
                return 0;
            }
            
            Stack<Integer> s = new Stack<>();
            int max = 0;
            
            for (int i = 0; i <= nums.length; i++) {
                int cur = i == nums.length ? -1 : nums[i];
                while (!s.isEmpty() && cur < nums[s.peek()]) {
                    int h = nums[s.pop()];
                    int w = s.isEmpty() ? i : i - s.peek() - 1;
                    max = Math.max(max, h * w);
                }
                s.push(i);
            }
            
            return max;
        }
        
        // transform to single rectangle
        private int[][] transform(char[][] matrix) {
            int n = matrix.length, m = matrix[0].length;
            int[][] nums = new int[n][m];
            // first row
            for (int j = 0; j < m; j++) {
                nums[0][j] = matrix[0][j] - '0';
            }
            //other
            for (int i = 1; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    nums[i][j] = matrix[i][j] == '0' ? 0 : nums[i - 1][j] + 1;
                }
            }
            return nums;
        }
    }
    

      

  • 相关阅读:
    SkyWalking链路追踪系统-告警篇
    在k8s中解决pod资源的正确识别
    SkyWalking链路追踪系统-接入篇
    Jenkins API+Pipeline深度实践之input的自动化
    SkyWalking链路追踪系统-部署篇
    DevOps建设之基于钉钉OA审批流的自动化上线
    使用kube-prometheus部署k8s监控(最新版)
    基于k8s手动部署rabbitmq集群
    ant desgin vue中table复选框根据状态disabled置灰
    ant design vue 中tree实现单选
  • 原文地址:https://www.cnblogs.com/harrygogo/p/4695190.html
Copyright © 2020-2023  润新知