{"id":22513699,"url":"https://github.com/emahtab/largest-rectangle-in-a-histogram","last_synced_at":"2026-01-29T15:32:28.403Z","repository":{"id":79525605,"uuid":"248955764","full_name":"eMahtab/largest-rectangle-in-a-histogram","owner":"eMahtab","description":"Largest rectangle in a histogram","archived":false,"fork":false,"pushed_at":"2020-04-05T05:52:31.000Z","size":17,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-05T22:19:12.343Z","etag":null,"topics":["leetcode","problems-solving"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-03-21T10:46:31.000Z","updated_at":"2020-04-05T05:52:33.000Z","dependencies_parsed_at":"2023-05-10T17:15:36.273Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/largest-rectangle-in-a-histogram","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/largest-rectangle-in-a-histogram","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flargest-rectangle-in-a-histogram","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flargest-rectangle-in-a-histogram/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flargest-rectangle-in-a-histogram/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flargest-rectangle-in-a-histogram/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/largest-rectangle-in-a-histogram/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flargest-rectangle-in-a-histogram/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28880233,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-29T10:31:27.438Z","status":"ssl_error","status_checked_at":"2026-01-29T10:31:01.017Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["leetcode","problems-solving"],"created_at":"2024-12-07T03:14:07.369Z","updated_at":"2026-01-29T15:32:28.399Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Largest rectangle in a histogram\n## https://leetcode.com/problems/largest-rectangle-in-histogram\n\nGiven 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.\n\n![Largest Rectangle in a histogram](histogram_area.png?raw=true \"Largest Rectangle in a histogram\")\n\nAbove 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.\n\n# Implementation 1 : O(n^2)\n```java\nclass Solution {\n    public int largestRectangleArea(int[] heights) {\n        if(heights == null || heights.length == 0)\n            return 0;\n        int area = heights[0];\n        for(int i = 0; i \u003c heights.length; i++) {\n            int minHeight = heights[i];\n            area = Math.max(area, heights[i]);\n            for(int j = i+1; j \u003c heights.length; j++) {\n                minHeight = Math.min(minHeight, heights[j]);\n                if(minHeight == 0)\n                    break;\n                area = Math.max(area, minHeight * (j - i +1));\n            }\n        }\n        return area;\n    }\n}\n```\n\n![Largest Rectangle in a histogram](histogram.PNG?raw=true \"Largest Rectangle in a histogram\")\n\n# Implementation 2 : O(n)\n```java\nclass Solution {\n    public int largestRectangleArea(int[] heights) {\n        Stack\u003cInteger\u003e stack = new Stack\u003c\u003e();\n        int maxArea = 0;\n        int index = 0;\n        while (index \u003c heights.length) {\n            if (stack.empty() || heights[index] \u003e= heights[stack.peek()]) {\n                stack.push(index++);\n            } else {\n                int top = stack.pop();\n                int width = stack.empty() ? index : index - stack.peek() - 1;\n                maxArea = Math.max(maxArea, heights[top] * width);\n            }\n        }\n\n        while (!stack.empty()) {\n            int top = stack.pop();\n            int width = stack.empty() ? index : index - stack.peek() - 1;\n            maxArea = Math.max(maxArea, heights[top] * width);\n        }\n\n       return maxArea;\n    }\n}\n```\n\n# References :\n1. https://leetcode.com/articles/largest-rectangle-in-histogram\n2. https://tech.pic-collage.com/algorithm-largest-area-in-histogram-84cc70500f0c\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Flargest-rectangle-in-a-histogram","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Flargest-rectangle-in-a-histogram","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Flargest-rectangle-in-a-histogram/lists"}