question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
painting-a-grid-with-three-different-colors
Rust | Bottom-up DP, 6ms, beats 100%
rust-bottom-up-dp-6ms-beats-100-by-soyfl-lvza
Intuition\nCompress each row into vertices, construct a graph and do DP on it.\n\n# Complexity\n- Time complexity:\nO(n)\n\n# Code\nrust\n#[derive(Debug, Clone,
soyflourbread
NORMAL
2024-03-05T02:13:26.747999+00:00
2024-03-05T02:13:26.748032+00:00
5
false
# Intuition\nCompress each row into vertices, construct a graph and do DP on it.\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n# Code\n```rust\n#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq)]\npub enum Color {\n Blue,\n Pink,\n White,\n}\n\npub fn to_vertices(\n width: usize,\n) -> Vec<Vec<Color>...
0
0
['Dynamic Programming', 'Rust']
0
painting-a-grid-with-three-different-colors
C++ bitmask DP
c-bitmask-dp-by-sanzenin_aria-aa0p
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
sanzenin_aria
NORMAL
2024-02-21T02:40:28.726197+00:00
2024-02-21T02:41:34.456216+00:00
23
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['C++']
0
painting-a-grid-with-three-different-colors
DP on Counter() base (without recursion) (60% | 81%)
dp-on-counter-base-without-recursion-60-et98b
Code\n\nclass Solution:\n \n def colorTheGrid(self, m: int, n: int) -> int:\n mod = 10 ** 9 + 7\n\n def get_first_line(m) -> list[str]:\n
eshved
NORMAL
2024-01-03T18:49:10.034401+00:00
2024-01-04T21:47:25.000592+00:00
16
false
# Code\n```\nclass Solution:\n \n def colorTheGrid(self, m: int, n: int) -> int:\n mod = 10 ** 9 + 7\n\n def get_first_line(m) -> list[str]:\n """Returns all possible (24) variations of the first line.\n """\n res = [[], [""]]\n for i in range(m):\n ...
0
0
['Python3']
0
painting-a-grid-with-three-different-colors
easy with all steps
easy-with-all-steps-by-parwez0786-5tjn
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
parwez0786
NORMAL
2024-01-02T12:49:33.462336+00:00
2024-01-02T12:49:33.462370+00:00
28
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['C++']
0
painting-a-grid-with-three-different-colors
python DP top down
python-dp-top-down-by-harrychen1995-04nc
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
harrychen1995
NORMAL
2023-11-13T19:25:12.504222+00:00
2023-11-13T19:25:12.504246+00:00
26
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['Dynamic Programming', 'Python3']
0
painting-a-grid-with-three-different-colors
Dynamic programming | Top to bottom | Easy to understand code
dynamic-programming-top-to-bottom-easy-t-h843
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
geeknkta
NORMAL
2023-11-01T15:13:38.051276+00:00
2023-11-01T15:13:38.051300+00:00
42
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['C++']
0
painting-a-grid-with-three-different-colors
C# Beats 100% in speed and memory usage
c-beats-100-in-speed-and-memory-usage-by-ug9o
\n\n\n# Complexity\n- Time complexity:\nO(n * (3^m)^2)\n\n- Space complexity:\nO(n * 3^m + (3^m)^2)\n\n# Code\n\npublic class Solution {\n public int ColorTh
junkmann
NORMAL
2023-10-19T01:00:19.324638+00:00
2023-10-19T01:00:19.324672+00:00
7
false
![image.png](https://assets.leetcode.com/users/images/36b511f7-83df-4e6e-8417-6cc67b17b11e_1697676819.7317235.png)\n\n\n# Complexity\n- Time complexity:\n$$O(n * (3^m)^2)$$\n\n- Space complexity:\n$$O(n * 3^m + (3^m)^2)$$\n\n# Code\n```\npublic class Solution {\n public int ColorTheGrid(int m, int n) {\n var ...
0
0
['C#']
0
painting-a-grid-with-three-different-colors
Java solution
java-solution-by-pejmantheory-735x
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
pejmantheory
NORMAL
2023-09-16T07:48:46.672162+00:00
2023-09-16T07:48:46.672194+00:00
50
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['Java']
0
painting-a-grid-with-three-different-colors
Top Down dp with bit masking Easy Complete Solution
top-down-dp-with-bit-masking-easy-comple-8aqo
My first thought was to just calculate No of possible paths to n-1, m-1 with given constrain but the thing went wrong was the other left remaining squires can f
demon_code
NORMAL
2023-09-06T08:28:03.361289+00:00
2023-09-06T13:02:09.588904+00:00
7
false
My first thought was to just calculate No of possible paths to n-1, m-1 with given constrain but the thing went wrong was the other left remaining squires can form different combination which is not part of selected path so it failed ``` :(```\n\nto colur any cell we need information about just upper sell and left cel...
0
0
['Dynamic Programming', 'Recursion', 'C', 'Bitmask']
0
painting-a-grid-with-three-different-colors
My Solution
my-solution-by-hope_ma-tdeg
\nclass Solution {\n public:\n int colorTheGrid(const int m, const int n) {\n constexpr int mod = 1000000007;\n constexpr int range = 2;\n constexpr i
hope_ma
NORMAL
2023-07-12T15:01:22.873630+00:00
2023-07-12T15:01:22.873653+00:00
7
false
```\nclass Solution {\n public:\n int colorTheGrid(const int m, const int n) {\n constexpr int mod = 1000000007;\n constexpr int range = 2;\n constexpr int color_mask = 0b11;\n /**\n * `0b00` stands for red\n * `0b01` stands for green\n * `0b10` stands for blue\n * `0b11` is invalid\n *...
0
0
[]
0
painting-a-grid-with-three-different-colors
No bitmask
no-bitmask-by-shivral-r0v2
\n Add your space complexity here, e.g. O(n) \n\n# Code\n\nb=[]\ndef gen(s,n):\n global b\n if len(s)==n:\n fl=False\n for i in range(1,n):\
shivral
NORMAL
2023-05-18T13:08:26.440412+00:00
2023-05-18T13:08:26.440441+00:00
50
false
\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nb=[]\ndef gen(s,n):\n global b\n if len(s)==n:\n fl=False\n for i in range(1,n):\n fl|=s[i]==s[i-1]\n if not fl:\n b.append(s)\n return\n gen(s+"0",n)\n gen(s+"1",n)\n gen(s+"2",n)\n...
0
0
['Python3']
0
painting-a-grid-with-three-different-colors
A concise solution with matrix ops only (beat 100% java submissions)
a-concise-solution-with-matrix-ops-only-i75mb
java\nclass Solution {\n public int colorTheGrid(int m, int n) {\n long mod = 1_000_000_007;\n long[][][] matrixs = {\n { { 2 }
safiir
NORMAL
2023-05-06T07:36:44.273201+00:00
2023-05-06T10:07:50.976974+00:00
101
false
```java\nclass Solution {\n public int colorTheGrid(int m, int n) {\n long mod = 1_000_000_007;\n long[][][] matrixs = {\n { { 2 } },\n { { 3 } },\n { { 3, 2 }, { 2, 2 } },\n {\n { 3, 2, 1, 2 },\n ...
0
0
['Dynamic Programming', 'Java']
0
painting-a-grid-with-three-different-colors
Python Solution
python-solution-by-aurimas13-cd3y
\n\n# Approach\nHere\'s a step-by-step explanation of the solution:\n\n1. Generate all valid rows:\nThe generate_valid_rows function generates all valid rows of
aurimas13
NORMAL
2023-03-15T07:20:15.852852+00:00
2023-03-15T07:20:15.852906+00:00
82
false
![Screenshot 2023-03-15 at 09.12.21.png](https://assets.leetcode.com/users/images/f75a5e94-b329-4550-a02d-d5a0bd78db12_1678864659.9028294.png)\n\n# Approach\nHere\'s a step-by-step explanation of the solution:\n\n1. Generate all valid rows:\nThe generate_valid_rows function generates all valid rows of length m, with no...
0
0
['Dynamic Programming', 'Graph', 'Python3']
0
painting-a-grid-with-three-different-colors
[Python 3] Bitmask DP
python-3-bitmask-dp-by-gabhay-2ich
Intuition\nsince m is very small so it seems sensible to use Bitmask DP here\n\n# Approach\nwe use first 5 bits for red, next 5 for blue and next 5 bits for gre
gabhay
NORMAL
2023-02-17T06:43:04.302906+00:00
2023-02-17T06:48:08.630739+00:00
56
false
# Intuition\nsince m is very small so it seems sensible to use Bitmask DP here\n\n# Approach\nwe use first 5 bits for red, next 5 for blue and next 5 bits for green\nand continously check for each position if we can use a specific color.\n\n# Time Compexity:\nO(n\\*m\\*2^(2\\*m))\n\n# Code\n```\nclass Solution:\n de...
0
0
['Python3']
0
painting-a-grid-with-three-different-colors
Python recursive DP, abstracted as a Graph counting problem
python-recursive-dp-abstracted-as-a-grap-b9pk
# Intuition \n\n\n\n\n\n# Complexity\nLet w := 3^m,\n- Time complexity: O(w^2m+w^2n)\n\n\n- Space complexity: O(w^2+wn)\n\n\n# Code\n\nM = 1_000_000_007\n\n\n
vWAj0nMjOtK33vIH0jpLww
NORMAL
2023-02-11T05:25:49.750950+00:00
2023-02-12T13:49:11.029090+00:00
57
false
<!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\nLet $$w := 3^m$$,\n- Time complexity: $$O(w^2m+w^2n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: ...
0
0
['Dynamic Programming', 'Graph', 'Python3']
1
painting-a-grid-with-three-different-colors
Easy solution in c++
easy-solution-in-c-by-doppelgangerofmeow-nxhv
Intuition\n Describe your first thoughts on how to solve this problem. \n1. Generateing all possible permutation using (m^3)\n2. Finding all permutation that co
doppelgangerOfMeow
NORMAL
2022-12-22T11:00:50.001202+00:00
2022-12-22T11:00:50.001247+00:00
226
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. Generateing all possible permutation using (m^3)\n2. Finding all permutation that could be promoted to\na. For example, 12 -> 21, 23 and etc.\n3. Using DP to record the value of transferring\n\n# Approach\n<!-- Describe your approach t...
0
0
['Dynamic Programming', 'Combinatorics', 'C++']
0
painting-a-grid-with-three-different-colors
Bottom-Up DP solution, beats 98% of cpp solutions
bottom-up-dp-solution-beats-98-of-cpp-so-s6e8
Beats 98.05% in Time and 99.35% in Space for cpp solutions\n\n\n\nclass Solution {\npublic:\n const int MOD = 1e9 + 7;\n void generateRows(vector<int> &ro
mosta7il
NORMAL
2022-07-20T19:08:54.224660+00:00
2022-07-20T19:08:54.224703+00:00
165
false
Beats **98.05%** in Time and **99.35%** in Space for cpp solutions\n![image](https://assets.leetcode.com/users/images/775029c7-7c97-4a8c-a93a-a718a67fe0f1_1658343994.58358.png)\n\n```\nclass Solution {\npublic:\n const int MOD = 1e9 + 7;\n void generateRows(vector<int> &rows, int idx, int row, int last, int &M){\...
0
0
['Dynamic Programming']
0
painting-a-grid-with-three-different-colors
C++ Recursion + Memo
c-recursion-memo-by-dhairya_sarin-i17a
\nclass Solution {\npublic:\n string color_scheme = "RYG";\n int mod;\n vector<string>valid_moves;\n unordered_map<string,int>dp;\n void generate
dhairya_sarin
NORMAL
2022-07-17T03:18:22.445928+00:00
2022-07-17T03:18:22.445956+00:00
180
false
```\nclass Solution {\npublic:\n string color_scheme = "RYG";\n int mod;\n vector<string>valid_moves;\n unordered_map<string,int>dp;\n void generateValidMoves(int n , int taken , char last, string &cur)\n { if(taken==n)\n {valid_moves.push_back(cur);\n return;\n }\n for(...
0
0
[]
0
painting-a-grid-with-three-different-colors
Simple DP Implementation
simple-dp-implementation-by-day_tripper-45ea
\nclass Solution(object):\n def colorTheGrid(self, m, n):\n """\n :type m: int\n :type n: int\n :rtype: int\n """\n
Day_Tripper
NORMAL
2022-07-03T04:03:06.582398+00:00
2022-07-03T04:03:06.582462+00:00
129
false
```\nclass Solution(object):\n def colorTheGrid(self, m, n):\n """\n :type m: int\n :type n: int\n :rtype: int\n """\n # m = 2\n\n patterns = ["a", "b", "c"]\n for i in range(m-1):\n new_patterns = []\n for p in patterns:\n ...
0
0
['Dynamic Programming']
0
painting-a-grid-with-three-different-colors
This is meaningless
this-is-meaningless-by-stevefan1999-03wa
This question is just asking for the 3-coloring chromatic polynomial of the (n, m)-grid graph where n <= 5 and m <= 1000...Which is unfortunately an open proble
stevefan1999
NORMAL
2022-06-30T08:22:58.900665+00:00
2022-06-30T08:23:40.446255+00:00
120
false
This question is just asking for the 3-coloring chromatic polynomial of the (n, m)-grid graph where n <= 5 and m <= 1000...Which is unfortunately an open problem.\n\nAnyone with Mathematica can brute force the formula. When the fuck did LC became an amalgamation of CodeForces and Project Euler?
0
0
[]
1
painting-a-grid-with-three-different-colors
How can I improve my code. I've used backtracking but I run into TLE in some test cases
how-can-i-improve-my-code-ive-used-backt-m20w
class Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n matrix = [[0]*n for _ in range(m)]\n neighbors = [(-1,0),(0,-1)]\n de
teapea
NORMAL
2022-06-30T04:28:57.875477+00:00
2022-06-30T04:28:57.875517+00:00
40
false
```class Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n matrix = [[0]*n for _ in range(m)]\n neighbors = [(-1,0),(0,-1)]\n def get_colors(r,c):\n colors = [1,2,3]\n ans = []\n if r==0 and c==0:\n return colors\n top = left ...
0
0
['Backtracking']
0
painting-a-grid-with-three-different-colors
Python. Faster than 83%...
python-faster-than-83-by-nag007-1jpw
\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n def f(i,p=-1,s=\'\'):\n if i==m:\n res.append(s)\n
nag007
NORMAL
2022-06-25T16:03:23.577373+00:00
2022-06-25T16:03:23.577408+00:00
84
false
```\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n def f(i,p=-1,s=\'\'):\n if i==m:\n res.append(s)\n return\n for j in range(3):\n if j!=p:\n f(i+1,j,s+str(j))\n @lru_cache(None)\n def fun...
0
0
[]
0
painting-a-grid-with-three-different-colors
Scala
scala-by-fairgrieve-4f02
\nimport scala.util.chaining.scalaUtilChainingOps\n\nobject Solution {\n private object Color extends Enumeration {\n type Color = Value\n val Red, Blue,
fairgrieve
NORMAL
2022-03-29T20:44:42.193528+00:00
2022-03-29T20:44:42.193554+00:00
53
false
```\nimport scala.util.chaining.scalaUtilChainingOps\n\nobject Solution {\n private object Color extends Enumeration {\n type Color = Value\n val Red, Blue, Green = Value\n }\n import Color._\n\n def colorTheGrid(m: Int, n: Int): Int = {\n val validCols = (1 to m).foldLeft(Iterable(List[Color]())) {\n ...
0
0
['Dynamic Programming', 'Scala']
0
painting-a-grid-with-three-different-colors
Fastest O(n) Python Solution
fastest-on-python-solution-by-vkarakchee-5e0i
The solution is completely similar to recurrent one of problem 1411. Memory complexity is O(1).\n\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -
vkarakcheev
NORMAL
2022-03-11T23:44:34.452308+00:00
2022-03-12T10:28:05.871929+00:00
181
false
The solution is completely similar to recurrent one of problem 1411. Memory complexity is O(1).\n```\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n\n if m == 1:\n return 3 * 2**(n-1) % 1000000007\n\n if m == 2:\n return 6 * 3**(n-1) % 1000000007\n\n if ...
0
0
[]
0
painting-a-grid-with-three-different-colors
[Python3] DP Rolling Array & Memo | 70% Time & 95% Space
python3-dp-rolling-array-memo-70-time-95-cvt3
\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n MOD = 10 ** 9 + 7\n # Get valid states for a column\n valid_states
pubghh01
NORMAL
2022-03-05T09:24:32.640384+00:00
2022-03-05T09:26:50.468718+00:00
162
false
```\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n MOD = 10 ** 9 + 7\n # Get valid states for a column\n valid_states = generate_states(m)\n # Init rolling array dp\n dp = [[0] * len(valid_states) for _ in range(2)]\n # Init the first column of dp to all...
0
0
['Dynamic Programming', 'Memoization']
0
painting-a-grid-with-three-different-colors
[Rust] DP & Mask runs in 88ms
rust-dp-mask-runs-in-88ms-by-bovinovain-flex
Realizing m <= 5 is crucial. It is possible to enuemrate all valid color permutation in a column and build dp based on it.\n\nuse std::collections::HashMap;\n\n
bovinovain
NORMAL
2022-03-01T13:50:58.215642+00:00
2022-03-01T13:50:58.215683+00:00
107
false
Realizing m <= 5 is crucial. It is possible to enuemrate all valid color permutation in a column and build dp based on it.\n```\nuse std::collections::HashMap;\n\nconst MOD: i32 = 1000_000_007;\nimpl Solution {\n pub fn color_the_grid(m: i32, n: i32) -> i32 {\n let mask = |mut x: usize| -> Vec<usize> {\n ...
0
0
[]
0
painting-a-grid-with-three-different-colors
Solution using unordered map C++
solution-using-unordered-map-c-by-aryan2-euvs
\nclass Solution {\npublic:\n //\n int mod=1e9+7;\n char arr[3]={\'R\',\'B\',\'G\'};\n vector<string>v;\n unordered_map<string,int>ans;\n void
aryan29
NORMAL
2022-02-22T18:07:23.834695+00:00
2022-02-22T18:07:23.834737+00:00
190
false
```\nclass Solution {\npublic:\n //\n int mod=1e9+7;\n char arr[3]={\'R\',\'B\',\'G\'};\n vector<string>v;\n unordered_map<string,int>ans;\n void get_v(int m, string s)\n {\n if(m==0)\n {\n v.push_back(s);\n return;\n }\n char ch=\'$\';\n if(...
0
0
[]
0
painting-a-grid-with-three-different-colors
DP with bitmask, similar to Prob 1349 Maximum Student Taking Exam
dp-with-bitmask-similar-to-prob-1349-max-s654
Consider each column, since it has much less valid states as compared to each row.\nThe below algorithm can also be extended to work for more than 3 colors scen
finback
NORMAL
2022-02-15T04:00:11.050004+00:00
2022-02-16T00:55:49.717974+00:00
172
false
Consider each column, since it has much less valid states as compared to each row.\nThe below algorithm can also be extended to work for more than 3 colors scenario\n\n```\nclass Solution { \n long[] mask;\n int idx = 0;\n // RRR => 001001001, RGR => 001010001, RGB => 001010100\n public int colorTheGrid(...
0
0
['Dynamic Programming', 'Bitmask']
0
painting-a-grid-with-three-different-colors
c++ | dp | graph
c-dp-graph-by-srv-er-pgcl
\nclass Solution {\npublic:\n int md=1000000007;\n vector<string> st;\n int dp[50][1001];\n void fill(int m,char prev,string s){\n if(m==0)st
srv-er
NORMAL
2022-02-15T01:19:25.558893+00:00
2022-02-15T01:19:25.558927+00:00
258
false
```\nclass Solution {\npublic:\n int md=1000000007;\n vector<string> st;\n int dp[50][1001];\n void fill(int m,char prev,string s){\n if(m==0)st.push_back(s);\n else{\n string t="RGB";\n for(auto&i:t){\n if(i!=prev) fill(m-1,i,s+i);\n } ...
0
0
['Dynamic Programming', 'Graph', 'C']
0
painting-a-grid-with-three-different-colors
JAVA Intuitive Solution
java-intuitive-solution-by-krishna382nit-gtpo
\nclass Solution {\n public int colorTheGrid(int m, int n) {\n Map<String, Integer> map=new HashMap();\n populateMap(map, m,"");\n if(n=
krishna382nitjsr
NORMAL
2022-02-06T20:47:52.531195+00:00
2022-02-06T20:48:20.154271+00:00
425
false
```\nclass Solution {\n public int colorTheGrid(int m, int n) {\n Map<String, Integer> map=new HashMap();\n populateMap(map, m,"");\n if(n==1){\n return map.size();\n }\n Map<String, List<String>> map1=new HashMap();\n for(String key:map.keySet()){\n po...
0
0
[]
0
painting-a-grid-with-three-different-colors
Golang DP solution
golang-dp-solution-by-tjucoder-7fcy
go\nfunc colorTheGrid(m int, n int) int {\n\tbase := make([]int, 0, m)\n\tfor i := 1; i <= 3; i++ {\n\t\tbase = append(base, i)\n\t}\n\tfor i := 2; i <= m; i++
tjucoder
NORMAL
2022-01-19T16:02:53.608334+00:00
2022-01-19T16:02:53.608365+00:00
119
false
```go\nfunc colorTheGrid(m int, n int) int {\n\tbase := make([]int, 0, m)\n\tfor i := 1; i <= 3; i++ {\n\t\tbase = append(base, i)\n\t}\n\tfor i := 2; i <= m; i++ {\n\t\tnewBase := make([]int, 0, 64)\n\t\tfor _, v := range base {\n\t\t\tfor j := 1; j <= 3; j++ {\n\t\t\t\tif j == v%10 {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\...
0
0
['Dynamic Programming', 'Go']
0
painting-a-grid-with-three-different-colors
Java Dp
java-dp-by-prathihaspodduturi-na4b
\nclass Solution {\n public void trav(int m,List<String>list,String s)\n {\n int len=s.length();\n if(len>1 && s.charAt(len-1)==s.charAt(len
prathihaspodduturi
NORMAL
2021-12-15T12:04:25.634355+00:00
2021-12-15T12:04:25.634400+00:00
386
false
```\nclass Solution {\n public void trav(int m,List<String>list,String s)\n {\n int len=s.length();\n if(len>1 && s.charAt(len-1)==s.charAt(len-2))\n return;\n if(m==0)\n {\n list.add(s);\n return;\n }\n trav(m-1,list,s+"r");\n trav...
0
0
[]
0
painting-a-grid-with-three-different-colors
[JavaScript] DP with Bit masking
javascript-dp-with-bit-masking-by-tmohan-x1yq
Time O(n * (2^m) * (2^m))\nSpace O(n * 2^(2 * m))\n\nvar colorTheGrid = function (m, n) {\n let dp = Array.from({ length: n }, () => Array(1 << (2 * m)));\n\
tmohan
NORMAL
2021-09-15T16:23:18.950901+00:00
2021-09-15T16:23:18.950933+00:00
130
false
**Time O(n * (2^m) * (2^m))\nSpace O(n * 2^(2 * m))**\n```\nvar colorTheGrid = function (m, n) {\n let dp = Array.from({ length: n }, () => Array(1 << (2 * m)));\n\n const mod = 1e9 + 7;\n\n const dfs = (rowIdx, colIdx, prevRowColor) => {\n if (rowIdx === n) return 1;\n\n if (colIdx === 0 && dp[r...
0
0
[]
0
painting-a-grid-with-three-different-colors
Easy DP Approach using UnorderedMap
easy-dp-approach-using-unorderedmap-by-o-oh2l
\n#define ll long long int\nconst int mod=1e9+7;\nclass Solution {\n int r,c;\n string curr;\n vector<string> comb; // all possible valid combinations
ojha1111pk
NORMAL
2021-09-12T16:30:30.160709+00:00
2021-09-12T16:30:30.160763+00:00
319
false
```\n#define ll long long int\nconst int mod=1e9+7;\nclass Solution {\n int r,c;\n string curr;\n vector<string> comb; // all possible valid combinations to color a column\n unordered_map<string,ll> dp[1003];\npublic:\n\t// finds all possible color combinations to color a given column\n void findCombinat...
0
0
['Dynamic Programming', 'C', 'C++']
0
largest-rectangle-in-histogram
5ms O(n) Java solution explained (beats 96%)
5ms-on-java-solution-explained-beats-96-skgtx
For any bar i the maximum rectangle is of width r - l - 1 where r - is the last coordinate of the bar to the right with height h[r] >= h[i] and l - is the last
anton4
NORMAL
2016-03-05T12:14:03+00:00
2018-10-21T01:39:43.212995+00:00
177,468
false
For any bar `i` the maximum rectangle is of width `r - l - 1` where r - is the last coordinate of the bar to the **right** with height `h[r] >= h[i]` and l - is the last coordinate of the bar to the **left** which height `h[l] >= h[i]`\n\nSo if for any `i` coordinate we know his utmost higher (or of the same height) ne...
1,357
12
['Dynamic Programming', 'Java']
125
largest-rectangle-in-histogram
AC Python clean solution using stack 76ms
ac-python-clean-solution-using-stack-76m-nmv1
def largestRectangleArea(self, height):\n height.append(0)\n stack = [-1]\n ans = 0\n for i in xrange(len(height)):\n whi
dietpepsi
NORMAL
2015-10-23T16:45:25+00:00
2018-10-24T11:02:29.968831+00:00
74,538
false
def largestRectangleArea(self, height):\n height.append(0)\n stack = [-1]\n ans = 0\n for i in xrange(len(height)):\n while height[i] < height[stack[-1]]:\n h = height[stack.pop()]\n w = i - stack[-1] - 1\n ans = max(ans, h * w)\n ...
646
6
['Python']
60
largest-rectangle-in-histogram
Short and Clean O(n) stack based JAVA solution
short-and-clean-on-stack-based-java-solu-s9u8
For explanation, please see https://bit.ly/2we8Wfx\n\n\n public int largestRectangleArea(int[] heights) {\n int len = heights.length;\n Stack<I
legendaryengineer
NORMAL
2015-01-19T20:51:19+00:00
2020-04-30T19:30:40.520578+00:00
120,429
false
For explanation, please see https://bit.ly/2we8Wfx\n\n```\n public int largestRectangleArea(int[] heights) {\n int len = heights.length;\n Stack<Integer> s = new Stack<>();\n int maxArea = 0;\n for (int i = 0; i <= len; i++){\n int h = (i == len ? 0 : heights[i]);\n ...
369
12
['Java']
66
largest-rectangle-in-histogram
Video Explanation
video-explanation-by-niits-vacy
Solution Video\n\nhttps://youtu.be/WTbLeCP8rwM\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to subscribe to my channel! \u2B50\uFE0F\u2B50\uFE0F\n\n\u25A0 Subs
niits
NORMAL
2024-06-27T14:29:21.596748+00:00
2024-08-17T12:42:16.409320+00:00
22,922
false
# Solution Video\n\nhttps://youtu.be/WTbLeCP8rwM\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to subscribe to my channel! \u2B50\uFE0F\u2B50\uFE0F\n\n**\u25A0 Subscribe URL**\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n\nSubscribers: 6,001\nThank you for your support!\n\n---\n\nhttps:/...
290
0
['C++', 'Java', 'Python3', 'JavaScript']
3
largest-rectangle-in-histogram
My concise C++ solution, AC 90 ms
my-concise-c-solution-ac-90-ms-by-sipipr-vlbv
I push a sentinel node back into the end of height to make the code logic more concise.\n \n\n class Solution {\n public:\n int largestR
sipiprotoss5
NORMAL
2014-10-09T01:18:19+00:00
2018-10-23T06:25:22.498889+00:00
89,874
false
I push a sentinel node back into the end of height to make the code logic more concise.\n \n\n class Solution {\n public:\n int largestRectangleArea(vector<int> &height) {\n \n int ret = 0;\n height.push_back(0);\n vector<int> index;\n ...
278
10
[]
39
largest-rectangle-in-histogram
[Java/C++] Explanation going from Brute to Optimal Approach
javac-explanation-going-from-brute-to-op-5m0n
\nWhat the Question is saying, \nGiven an array of integers heights representing the histogram\'s bar height where the width of each bar is 1, \nreturn the area
hi-malik
NORMAL
2022-01-29T05:17:20.078713+00:00
2022-08-03T11:57:13.226306+00:00
22,120
false
```\nWhat the Question is saying, \nGiven an array of integers heights representing the histogram\'s bar height where the width of each bar is 1, \nreturn the area of the largest rectangle in the histogram.\n```\n\nUnderstand this problem with an example;\n**Input:** `heights = [2,1,5,6,2,3]`\n![image](https://assets.l...
264
56
['C', 'Java']
16
largest-rectangle-in-histogram
[Python] increasing stack, explained
python-increasing-stack-explained-by-dba-gzco
Key insight: for given bar i with value h, how to find the biggest rectangle, which is ending with this bar? We need to find the previous smallest place, where
dbabichev
NORMAL
2020-12-31T10:24:02.552167+00:00
2020-12-31T13:01:27.646571+00:00
12,983
false
**Key insight**: for given bar `i` with value `h`, how to find the biggest rectangle, which is ending with this bar? We need to find the previous smallest place, where value is less or equal than `h`: that is smallest `j`, such that `j < i` and `heights[j] >= heights[i]`. Ideal data structure for these type of problem...
202
4
['Monotonic Stack']
18
largest-rectangle-in-histogram
Segment tree solution, just another idea, O(N*logN) Solution
segment-tree-solution-just-another-idea-4xuan
Inspired by this solution:\nhttp://www.geeksforgeeks.org/largest-rectangular-area-in-a-histogram-set-1/\n\nJust wanna to provide another idea of how to solve th
xiaohui5319
NORMAL
2016-05-20T05:40:14+00:00
2016-05-20T05:40:14+00:00
31,481
false
Inspired by this solution:\nhttp://www.geeksforgeeks.org/largest-rectangular-area-in-a-histogram-set-1/\n\nJust wanna to provide another idea of how to solve this question in O(N*logN) theoretically, 40 ms solution:\n\n // Largest Rectangle in Histogram\n // Stack solution, O(NlogN) solution\n \n class SegT...
191
0
['C++']
112
largest-rectangle-in-histogram
AC clean Java solution using stack
ac-clean-java-solution-using-stack-by-je-e0le
public int largestRectangleArea(int[] h) {\n int n = h.length, i = 0, max = 0;\n \n Stack<Integer> s = new Stack<>();\n \n while (i
jeantimex
NORMAL
2015-09-13T23:10:32+00:00
2018-10-01T15:36:57.399300+00:00
21,543
false
public int largestRectangleArea(int[] h) {\n int n = h.length, i = 0, max = 0;\n \n Stack<Integer> s = new Stack<>();\n \n while (i < n) {\n // as long as the current bar is shorter than the last one in the stack\n // we keep popping out the stack and calculate the area ba...
190
5
['Java']
12
largest-rectangle-in-histogram
Simple Divide and Conquer AC solution without Segment Tree
simple-divide-and-conquer-ac-solution-wi-9het
The idea is simple: for a given range of bars, the maximum area can either from left or right half of the bars, or from the area containing the middle two bars.
gildorwang
NORMAL
2015-01-17T12:40:03+00:00
2018-08-29T06:19:37.340555+00:00
23,801
false
The idea is simple: for a given range of bars, the maximum area can either from left or right half of the bars, or from the area containing the middle two bars. For the last condition, expanding from the middle two bars to find a maximum area is `O(n)`, which makes a typical Divide and Conquer solution with `T(n) = 2T(...
144
1
['Divide and Conquer', 'C++']
15
largest-rectangle-in-histogram
Python solution without using stack. (with explanation)
python-solution-without-using-stack-with-sb7b
The algorithm using stack actually is not quite intuitive. At least I won't think about using it at first time.\n\nIt is more natural to think about this way. L
samuri
NORMAL
2016-10-04T15:21:36.608000+00:00
2018-08-26T23:38:45.618608+00:00
11,670
false
The algorithm using stack actually is not quite intuitive. At least I won't think about using it at first time.\n\nIt is more natural to think about this way. Let left[i] to indicate how many bars to the left (including the bar at index i) are equal or higher than bar[i], right[i] is that to the right of bar[i], so the...
117
2
[]
24
largest-rectangle-in-histogram
44ms Easy Solution | C++
44ms-easy-solution-c-by-giriteja94495-suau
This method is not very intuitive ..i did this with some previous knowledge of similar problems\n\nclass Solution {\npublic:\n int largestRectangleArea(vecto
giriteja94495
NORMAL
2020-07-12T15:29:57.187789+00:00
2020-07-12T15:32:05.174240+00:00
12,091
false
This method is not very intuitive ..i did this with some previous knowledge of similar problems\n```\nclass Solution {\npublic:\n int largestRectangleArea(vector<int>& heights) {\n // this problem should be solved using stack .\n /* whenever you see a monotonic increase in the input which \n\t\two...
100
4
['Stack', 'C', 'C++']
11
largest-rectangle-in-histogram
✔️ [Python3] MONOTONIC STACK t(-_-t), Explained
python3-monotonic-stack-t-_-t-explained-jd0a8
UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.\n\nThe idea is to use a monotonic stack. We iterate over bars a
artod
NORMAL
2022-01-29T03:35:24.745577+00:00
2022-01-29T09:01:31.260792+00:00
11,526
false
**UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.**\n\nThe idea is to use a monotonic stack. We iterate over bars and add them to the stack as long as the last element in the stack is less than the current bar. When the condition doesn\'t hold, we start to calculate area...
90
5
['Monotonic Stack', 'Python3']
13
largest-rectangle-in-histogram
All 4 approaches shown || O(N^2) || O(NlogN) || O(N)(two pass) || O(N)(one pass) with explanation
all-4-approaches-shown-on2-onlogn-ontwo-cnl6w
I will try to explain all the three approaches I know for solving this problem : \n1) Brute Force O(n^2)(too slow TLE)\nwe can compute area of every subarray a
sharan_17
NORMAL
2022-01-29T08:21:30.900015+00:00
2022-01-29T08:22:08.092344+00:00
9,048
false
I will try to explain all the three approaches I know for solving this problem : \n**1) Brute Force O(n^2)**(too slow **TLE**)\nwe can compute area of every subarray and return the max of them all.\n```\nclass Solution {\npublic:\n int largestRectangleArea(vector<int>& heights) {\n int n = heights.size() ; \...
81
2
['Divide and Conquer', 'Stack', 'Tree', 'Recursion', 'C']
3
largest-rectangle-in-histogram
📊 Monotonique Stack Solution Intuition (Javascript)
monotonique-stack-solution-intuition-jav-ehn8
Oh, hard problem it is, isn\'t it? \nLet\'s develop some intuition on how to access it.\n\n## Increasing \nWhen the heights are increasing, we can always build
avakatushka
NORMAL
2021-08-28T10:20:59.953224+00:00
2021-08-28T10:24:12.689723+00:00
3,829
false
Oh, **hard problem** it is, isn\'t it? \nLet\'s develop some intuition on how to access it.\n\n## Increasing \nWhen the heights are **increasing**, we can always build rectangle from a point there a previous height started, to the current point. \n![image](https://assets.leetcode.com/users/images/5b931e15-5391-4cd5-bed...
68
0
['Stack', 'JavaScript']
8
largest-rectangle-in-histogram
My concise code (20ms, stack based, O(n)), one trick used
my-concise-code-20ms-stack-based-on-one-o622x
The idea is simple, use a stack to save the index of each vector entry in a ascending order; once the current entry is smaller than the one with the index s.top
lejas
NORMAL
2015-05-21T01:59:00+00:00
2018-10-08T20:08:44.116083+00:00
15,181
false
The idea is simple, use a stack to save the index of each vector entry in a ascending order; once the current entry is smaller than the one with the index s.top(), then that means the rectangle with the height height[s.top()] ends at the current position, so calculate its area and update the maximum. \nThe only trick I...
55
2
[]
7
largest-rectangle-in-histogram
[C++] Simple Solution | Brute Force ▶️ Optimal | W/ Explanation
c-simple-solution-brute-force-optimal-w-7oax0
APPROACH 1 : BRUTE FORCE\n\n A Brute force approach would be to one by one consider all bars as starting points and calculate area of all rectangles starting wi
Mythri_Kaulwar
NORMAL
2022-01-29T02:54:46.629012+00:00
2022-01-29T17:31:53.720248+00:00
5,796
false
**APPROACH 1 : BRUTE FORCE**\n\n* A Brute force approach would be to one by one consider all bars as starting points and calculate area of all rectangles starting with every bar. \n* Finally return maximum of all possible areas. \n\n**Time complexity :** O(n^2)\n\n* We can use Divide and Conquer to solve this in O(nLog...
48
4
['Stack', 'C', 'C++']
3
largest-rectangle-in-histogram
🔥BEATS 💯 % 🎯 |✨SUPER EASY BEGINNERS 👏
beats-super-easy-beginners-by-codewithsp-83a9
\n\n---\n\n\n---\n# Intuition\nThe problem can be visualized as calculating the largest rectangle that can be formed in a histogram. The intuition is to use a s
CodeWithSparsh
NORMAL
2024-12-08T15:53:06.109952+00:00
2024-12-08T16:32:23.281582+00:00
5,178
false
![image.png](https://assets.leetcode.com/users/images/adb45702-45b0-4eae-a736-8b972f8edb5e_1733673154.8086953.png)\n\n---\n![1000028614.png](https://assets.leetcode.com/users/images/8640e877-1d52-4a8d-bd2b-2ba3f5adb533_1733675517.6725366.png)\n\n---\n# Intuition\nThe problem can be visualized as calculating the largest...
40
0
['Array', 'Stack', 'C', 'Monotonic Stack', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'Dart']
2
largest-rectangle-in-histogram
6 Similar questions || Prev smaller - Next smaller || Monotonic stack || C++
6-similar-questions-prev-smaller-next-sm-0na3
1. Largest Rectangle in a Histogram: https://leetcode.com/problems/largest-rectangle-in-histogram/\n\n\nclass Solution {\npublic:\n int largestRectangleArea(
hyperVJ
NORMAL
2021-11-30T07:38:37.092811+00:00
2021-11-30T07:38:37.092839+00:00
2,824
false
**1. Largest Rectangle in a Histogram**: https://leetcode.com/problems/largest-rectangle-in-histogram/\n\n```\nclass Solution {\npublic:\n int largestRectangleArea(vector<int>& heights) {\n int n = heights.size();\n vector<int> nsl(n),psl(n);\n \n stack<int> s;\n \n for(int ...
39
0
['C', 'Monotonic Stack']
4
largest-rectangle-in-histogram
[Python] Monotone Increasing Stack, Similar problems attached
python-monotone-increasing-stack-similar-iivn
\nclass Solution:\n def largestRectangleArea(self, heights: List[int]) -> int:\n ## RC ##\n ## APPROACH : MONOTONOUS INCREASING STACK ##\n
101leetcode
NORMAL
2020-06-15T10:55:23.819071+00:00
2020-06-15T10:56:07.575536+00:00
6,365
false
```\nclass Solution:\n def largestRectangleArea(self, heights: List[int]) -> int:\n ## RC ##\n ## APPROACH : MONOTONOUS INCREASING STACK ##\n ## Similar to Leetcode: 1475. Final Prices With a Special Discount in a Shop ##\n ## Similar to Leetcode: 907. Sum Of Subarray Minimums ##\n ...
39
0
['Python', 'Python3']
6
largest-rectangle-in-histogram
C++ solution, clean code
c-solution-clean-code-by-lchen77-j4x7
int largestRectangleArea(vector<int>& height) {\n height.push_back(0);\n const int size_h = height.size();\n stack<int> stk;\n int i
lchen77
NORMAL
2015-08-09T19:53:20+00:00
2015-08-09T19:53:20+00:00
8,215
false
int largestRectangleArea(vector<int>& height) {\n height.push_back(0);\n const int size_h = height.size();\n stack<int> stk;\n int i = 0, max_a = 0;\n while (i < size_h) {\n if (stk.empty() || height[i] >= height[stk.top()]) stk.push(i++);\n else {\n ...
38
2
[]
7
largest-rectangle-in-histogram
✅C++ 100% || Worst to Best approaches with explanation || Easy to understand.
c-100-worst-to-best-approaches-with-expl-6px4
Read the below solutions to understand the logic.\n\nPlease upvote if you like it\n\nSolution 1: Brute Force Approach (TLE)\n\nIntuition: The intuition behind t
pranjal9424
NORMAL
2022-09-15T11:48:43.784943+00:00
2022-09-15T16:37:23.034216+00:00
3,918
false
**Read the below solutions to understand the logic.**\n\n***Please upvote if you like it***\n\n**Solution 1: Brute Force Approach (TLE)**\n\n**Intuition:** The intuition behind the approach is taking different bars and finding the maximum width possible using the bar.\n\n![image](https://assets.leetcode.com/users/image...
33
0
['Stack', 'C', 'Monotonic Stack', 'C++']
4
largest-rectangle-in-histogram
My modified answer from GeeksforGeeks, in JAVA
my-modified-answer-from-geeksforgeeks-in-bhd5
I was stuck and took an eye on Geeks4Geeks. I got the idea and tried to figure it out by myself...\nIt takes me a lot of time to make it through....\n\nEDITED:
reeclapple
NORMAL
2014-07-25T03:24:19+00:00
2014-07-25T03:24:19+00:00
18,683
false
I was stuck and took an eye on Geeks4Geeks. I got the idea and tried to figure it out by myself...\nIt takes me a lot of time to make it through....\n\n**EDITED:** Now it is pretty concise....\n\n public class Solution {\n public int largestRectangleArea(int[] height) {\n if (height==null) return 0;//Shoul...
33
2
['Java']
7
largest-rectangle-in-histogram
✔ C++ || 💯 Explanation using Stack || O(N) time, space
c-explanation-using-stack-on-time-space-6jg4z
Problem:\n- We are given an array of integers which represent the height of bars in a histogram.\n- The width of each bar is 1.\n- We need to form a rectangle w
GodOfm4a1
NORMAL
2023-04-20T06:45:02.441183+00:00
2023-04-20T06:47:15.047370+00:00
2,150
false
Problem:\n- We are given an array of integers which represent the height of bars in a histogram.\n- The width of each bar is 1.\n- We need to form a rectangle with these bars with the largest area.\n\nExample :\n\n![WhatsApp Image 2023-04-20 at 10.54.15 AM.jpeg](https://assets.leetcode.com/users/images/f503d54d-30ce-43...
28
0
['Stack', 'Monotonic Stack', 'Simulation', 'C++']
0
largest-rectangle-in-histogram
Beats 99%✅ | O( n )✅ | Python (Step by step explanation)
beats-99-o-n-python-step-by-step-explana-0oah
Intuition\nThe problem involves finding the largest rectangle that can be formed in a histogram of different heights. The approach is to iteratively process the
monster0Freason
NORMAL
2023-10-16T17:09:51.462734+00:00
2023-11-05T20:28:52.990267+00:00
3,439
false
# Intuition\nThe problem involves finding the largest rectangle that can be formed in a histogram of different heights. The approach is to iteratively process the histogram\'s bars while keeping track of the maximum area found so far.\n\n# Approach\n1. Initialize a variable `maxArea` to store the maximum area found, an...
27
0
['Stack', 'Python3']
3
largest-rectangle-in-histogram
Javascript using stack (Detail breakdown)
javascript-using-stack-detail-breakdown-ilmb1
Do I explain this good enough?``\njs\n/**\n * @param {number[]} heights\n * @return {number}\n */\nconst largestRectangleArea = function (heights) {\n let ma
harikirito
NORMAL
2020-03-03T07:31:04.095360+00:00
2020-03-03T07:34:13.945507+00:00
2,729
false
Do I explain this good enough?``\n```js\n/**\n * @param {number[]} heights\n * @return {number}\n */\nconst largestRectangleArea = function (heights) {\n let maxArea = 0;\n const stack = [];\n // Append shadow rectangle (height = 0) to both side\n heights = [0].concat(heights).concat([0]);\n for (let i =...
26
0
['JavaScript']
6
largest-rectangle-in-histogram
O(n) stack c++ solution 12ms 中文详细解释
on-stack-c-solution-12ms-zhong-wen-xiang-s5wb
\u601D\u8DEF\u662F\n\n\u56E0\u4E3A\u76F8\u90BB\u7684\u6BD4curr\u9AD8\u7684\u5757\u80AF\u5B9A\u53EF\u4EE5\u4F5C\u4E3A \u9AD8\u5EA6\u4E3Aheights[curr]\u7684\u957F
alanzwy
NORMAL
2019-01-12T07:08:22.886392+00:00
2019-01-12T07:08:22.886437+00:00
2,866
false
\u601D\u8DEF\u662F\n\n\u56E0\u4E3A\u76F8\u90BB\u7684\u6BD4curr\u9AD8\u7684\u5757\u80AF\u5B9A\u53EF\u4EE5\u4F5C\u4E3A \u9AD8\u5EA6\u4E3Aheights[curr]\u7684\u957F\u65B9\u5F62\u7684\u4E00\u90E8\u5206\n\u6240\u4EE5\u89C2\u5BDF\u5230\u9AD8\u5EA6\u4E3Aheights[curr]\u7684\u957F\u65B9\u5F62\u7684\u6700\u5927\u9762\u79EF\u662F ...
23
3
[]
4
largest-rectangle-in-histogram
C++ | Stack | Next Smaller Element
c-stack-next-smaller-element-by-reetisha-bbtx
\nclass Solution {\npublic:\n vector<int> nextSmallerRight(vector<int> heights, int n){\n vector<int> res(n);\n stack<int> st;\n \n
reetisharma
NORMAL
2021-10-07T13:30:04.364779+00:00
2021-10-24T17:50:23.039312+00:00
2,022
false
```\nclass Solution {\npublic:\n vector<int> nextSmallerRight(vector<int> heights, int n){\n vector<int> res(n);\n stack<int> st;\n \n for(int i=n-1; i>=0; i--){\n while(!st.empty() && heights[st.top()]>=heights[i])\n st.pop();\n if(st.empty())\n ...
20
1
['Stack', 'C']
4
largest-rectangle-in-histogram
C++/Java/Python/JavaScript || ✅🚀 Stack || ✔️🔥TC : O(n)
cjavapythonjavascript-stack-tc-on-by-dev-mbni
Intuition:\nThe intuition behind the solution is to use two arrays, nsr (nearest smaller to the right) and nsl (nearest smaller to the left), to determine the b
devanshupatel
NORMAL
2023-05-13T13:52:30.722382+00:00
2023-05-13T13:52:30.722413+00:00
5,194
false
# Intuition:\nThe intuition behind the solution is to use two arrays, `nsr` (nearest smaller to the right) and `nsl` (nearest smaller to the left), to determine the boundaries of the rectangle for each bar in the histogram. By calculating the area for each rectangle and keeping track of the maximum area, we can find th...
19
0
['Monotonic Stack', 'Python', 'C++', 'Java', 'JavaScript']
1
largest-rectangle-in-histogram
Java | TC: O(N) | SC: O(N) | Optimal Stack solution
java-tc-on-sc-on-optimal-stack-solution-tkik8
java\n/**\n * Using stack to save the increasing height index.\n *\n * Time Complexity: O(N) --> Each element is visited maximum twice. (Once pushed\n * in stac
NarutoBaryonMode
NORMAL
2021-10-13T10:00:52.891549+00:00
2021-10-13T10:09:07.735125+00:00
2,316
false
```java\n/**\n * Using stack to save the increasing height index.\n *\n * Time Complexity: O(N) --> Each element is visited maximum twice. (Once pushed\n * in stack and once popped for stack)\n *\n * Space Complexity: O(N)\n *\n * N = Length of the input array.\n */\nclass Solution {\n public int largestRectangleAre...
18
0
['Array', 'Stack', 'Java']
3
largest-rectangle-in-histogram
Share my 2ms Java solution. Beats 100% Java submissions
share-my-2ms-java-solution-beats-100-jav-tivk
public class Solution {\n public int largestRectangleArea(int[] heights) {\n if (heights == null || heights.length == 0) return 0;\n
ya_ah
NORMAL
2016-03-13T19:38:49+00:00
2016-03-13T19:38:49+00:00
6,010
false
public class Solution {\n public int largestRectangleArea(int[] heights) {\n if (heights == null || heights.length == 0) return 0;\n return getMax(heights, 0, heights.length);\n } \n int getMax(int[] heights, int s, int e) {\n if (s + 1 >= e) return heights[s...
17
2
['Java']
9
largest-rectangle-in-histogram
Solution Using Stack(C++)
solution-using-stackc-by-surabhi1997mitr-gc9x
This problem is particularly almost similar to Trapping Rainwater Problem.\nHere in this post I would like to highlight both their solutions so one could dop a
surabhi1997mitra
NORMAL
2021-04-04T15:33:22.036940+00:00
2021-04-04T15:33:22.036974+00:00
1,717
false
This problem is particularly almost similar to Trapping Rainwater Problem.\nHere in this post I would like to highlight both their solutions so one could dop a comparitive study of the approach and figure out their individual details.\n\n**Trapping Rainwater Problem (Approach Using Stack)**\n\n```\nint trap(vector<int...
15
3
['Stack', 'C']
0
largest-rectangle-in-histogram
✔️ 100% Fastest Swift Solution, time: O(n), space: O(n).
100-fastest-swift-solution-time-on-space-8gro
\nclass Solution {\n // - Complexity:\n // - time: O(n), where n is the length of heights.\n // - space: O(n), where n is the length of heights.\n\
sergeyleschev
NORMAL
2022-04-07T05:44:40.681628+00:00
2022-04-07T05:44:40.681678+00:00
642
false
```\nclass Solution {\n // - Complexity:\n // - time: O(n), where n is the length of heights.\n // - space: O(n), where n is the length of heights.\n\n func largestRectangleArea(_ heights: [Int]) -> Int {\n var stack = [Int]()\n let n = heights.count\n var ans = 0\n \n ...
14
0
['Swift']
0
largest-rectangle-in-histogram
Stack C++ - Easy to understand (180ms) [2 Approaches]
stack-c-easy-to-understand-180ms-2-appro-gnft
Here are two approaches for the problem:\n1. We use previous smaller element and next smaller element and compare those two to \n\tget our desired max rectangle
NeerajSati
NORMAL
2022-01-29T04:31:26.395379+00:00
2022-01-29T05:26:16.505051+00:00
1,226
false
Here are two approaches for the problem:\n1. We use previous smaller element and next smaller element and compare those two to \n\tget our desired max rectangle area.\n\t\n\tWe have to use two separate functions here to find the previous smaller and next smaller element.\n\tThe algorithm to make it happen is this:\n\t`...
14
0
['Stack', 'C']
2
largest-rectangle-in-histogram
Python Solution Using Monotonic Stack With Explanation
python-solution-using-monotonic-stack-wi-hfaj
Problem Description\n\nThe problem requires finding the area of the largest rectangle that can be formed in a histogram represented by an array of non-negative
KhadimHussainDev
NORMAL
2024-04-13T07:29:36.314301+00:00
2024-04-14T11:40:50.533084+00:00
2,856
false
## Problem Description\n\nThe problem requires finding the area of the largest rectangle that can be formed in a histogram represented by an array of non-negative integers `heights`, where each bar\'s width is 1 unit.\n\n## Approach Explanation\n\nThis solution uses a stack-based approach to calculate the maximum recta...
13
1
['Stack', 'Monotonic Stack', 'Python3']
4
largest-rectangle-in-histogram
My C++ DP solution, 16ms, easy to understand!
my-c-dp-solution-16ms-easy-to-understand-z7k8
I think it might be O(n), or very close to O(n), it cannot be O(n^2).\nCan anybody help to prove?\n\n\n int largestRectangleArea(vector& height) {\n i
phu1ku
NORMAL
2015-07-21T09:57:29+00:00
2015-07-21T09:57:29+00:00
4,734
false
I think it might be O(n), or very close to O(n), it cannot be O(n^2).\nCan anybody help to prove?\n\n\n int largestRectangleArea(vector<int>& height) {\n int n = height.size(), ans = 0, p;\n vector<int> left(n,0), right(n,n);\n for (int i = 1;i < n;++i) {\n p = i-1;\n while...
13
0
['C++']
7
largest-rectangle-in-histogram
Detailed explanation of this problem O(n)
detailed-explanation-of-this-problem-on-7xs0a
Idea 1:\n\tDivide and Conquer using segment tree search. Time & space O(nlgn)\n\thttp://www.geeksforgeeks.org/largest-rectangular-area-in-a-histogram-set-1/\n\t
zehua2
NORMAL
2016-08-19T23:54:49.344000+00:00
2016-08-19T23:54:49.344000+00:00
1,525
false
Idea 1:\n\tDivide and Conquer using segment tree search. Time & space O(nlgn)\n\thttp://www.geeksforgeeks.org/largest-rectangular-area-in-a-histogram-set-1/\n\t\n\t\nIdea 2:\n\tFor each bar, we calculate maximum area with current bar as the smallest one in the rectangle. If we want to maximize the area of this rectangl...
13
0
[]
2
largest-rectangle-in-histogram
Largest Rectangle in Histogram - Easy Approach explained with images
largest-rectangle-in-histogram-easy-appr-lqd0
Problem Statement Explained\n Describe your first thoughts on how to solve this problem. \nThis question wants to find the largest rectangle (ie largest area) t
ak9807
NORMAL
2024-07-14T11:48:17.047177+00:00
2024-07-15T14:16:21.751520+00:00
3,137
false
# Problem Statement Explained\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis question wants to find the largest rectangle (ie largest area) that can be formed in the given histogram.\nWe are given an array of heights of the histogram bars and width of all is supposed to be 1 unit.\n\n---\n\n...
12
0
['Array', 'Stack', 'Monotonic Stack', 'Python', 'Java']
1
largest-rectangle-in-histogram
[JAVA] Clean Code, O(N) Solution Using Stack Data Structure
java-clean-code-on-solution-using-stack-wy9b9
\nclass Solution {\n public int largestRectangleArea(int[] heights) {\n \n int maximumRectangle = 0;\n Stack<Integer> stack = new Stack<
anii_agrawal
NORMAL
2020-09-24T19:05:43.440043+00:00
2021-04-28T19:35:10.095185+00:00
1,003
false
```\nclass Solution {\n public int largestRectangleArea(int[] heights) {\n \n int maximumRectangle = 0;\n Stack<Integer> stack = new Stack<> ();\n \n for (int i = 0; i <= heights.length; i++) {\n while (!stack.isEmpty () && (i == heights.length || heights[stack.peek ()] ...
12
1
['Stack', 'Java']
1
largest-rectangle-in-histogram
faster than 88.25% [easy-understanding][c++][stack]
faster-than-8825-easy-understandingcstac-xy68
\n class Solution {\n public:\n int largestRectangleArea(vector& heights) {\n stack s;\n in
rajat_gupta_
NORMAL
2020-08-20T06:47:31.067136+00:00
2020-08-20T07:02:14.060119+00:00
1,179
false
\n class Solution {\n public:\n int largestRectangleArea(vector<int>& heights) {\n stack<int> s;\n int maxArea = 0;\n int area = 0;\n int i;\n for(i=0; i < heights.size();){\n ...
12
1
['Stack', 'C', 'C++']
0
largest-rectangle-in-histogram
Java solution with explanations in Chinese
java-solution-with-explanations-in-chine-npra
S1:\u53CC\u91CD\u904D\u5386\u6CD5\n\n\u672C\u9898\u8981\u6C42\u7684\u662F\u4E00\u6BB5\u8FDE\u7EED\u7684\u77E9\u5F62\uFF0C\u80FD\u591F\u7EC4\u6210\u7684\u9762\u7
wuruoye
NORMAL
2019-01-25T06:56:33.228332+00:00
2019-01-25T06:56:33.228376+00:00
947
false
S1:\u53CC\u91CD\u904D\u5386\u6CD5\n\n\u672C\u9898\u8981\u6C42\u7684\u662F\u4E00\u6BB5\u8FDE\u7EED\u7684\u77E9\u5F62\uFF0C\u80FD\u591F\u7EC4\u6210\u7684\u9762\u79EF\u6700\u5927\u7684\u77E9\u5F62\u7684\u9762\u79EF\uFF0C\u6240\u4EE5\uFF0C\u53EA\u8981\u80FD\u591F\u6C42\u51FA\u8FD9\u4E00\u6BB5\u77E9\u5F62\u7684\u4F4D\u7F6E\...
12
3
[]
6
largest-rectangle-in-histogram
[Python] Stack Solution O(N) Time - using approach of Next Smaller to Left and Right
python-stack-solution-on-time-using-appr-p92u
\nclass Solution:\n def largestRectangleArea(self, heights: List[int]) -> int:\n n = len(heights)\n # left boundary => next smaller element to
SamirPaulb
NORMAL
2022-05-28T09:38:51.882768+00:00
2022-05-28T09:42:14.487702+00:00
2,418
false
```\nclass Solution:\n def largestRectangleArea(self, heights: List[int]) -> int:\n n = len(heights)\n # left boundary => next smaller element to left\n stack = []\n nextSmallerLeft = [0]*n\n for i in range(n):\n while stack and heights[stack[-1]] >= heights[i]:\n ...
11
0
['Stack', 'Monotonic Stack', 'Python', 'Python3']
3
largest-rectangle-in-histogram
[C++] O(n) Stack solution || Explaination with diagram
c-on-stack-solution-explaination-with-di-9en6
Approach:\nArea of reactangle is height * width\nSince we want to maximise the area , we can either increase height or width. But since the heights are already
iShubhamRana
NORMAL
2022-01-29T05:36:00.087881+00:00
2022-01-29T05:38:30.909711+00:00
629
false
**Approach:**\nArea of reactangle is height * width\nSince we want to maximise the area , we can either increase height or width. But since the heights are already fixed , we need to find the maximum width for each height,\n\nThe naive approach to implement this is to traverse and for each height traverse to left and r...
11
0
['Monotonic Stack']
0
largest-rectangle-in-histogram
✅C++ solution using Stack & Arrays with full explanations
c-solution-using-stack-arrays-with-full-0j6vx
If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistake please let me know. Thank you!\u27
dhruba-datta
NORMAL
2022-01-22T13:06:46.186991+00:00
2022-01-22T17:45:09.380765+00:00
760
false
> **If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistake please let me know. Thank you!\u2764\uFE0F**\n> \n\n---\n## Explanation:\n\n### Solution 01\n\n- Here we\u2019ll take a stack & 2 arrays to solve it.\n- We\u2019ll find the left and right sm...
11
0
['C', 'C++']
1
largest-rectangle-in-histogram
Python solution
python-solution-by-zitaowang-fu9r
Initialize an empty stack and iterate over the elements in heights. Suppose we are at index i, we can construct stack in such a way that it contains all the rec
zitaowang
NORMAL
2021-01-19T04:07:32.835863+00:00
2021-01-19T04:08:24.256004+00:00
846
false
Initialize an empty `stack` and iterate over the elements in `heights`. Suppose we are at index `i`, we can construct `stack` in such a way that it contains all the rectangles that started at an index `j <= i`, and can survive `i` (i.e., `height[j] <= height[i]`). By construction, the rectangles in `stack` will have a ...
11
0
[]
1
largest-rectangle-in-histogram
TLE for test case 3000 consecutive 1s
tle-for-test-case-3000-consecutive-1s-by-ef9q
It passes the custom testcase in 24ms, but when I submit it's TLE..\n\nCode:\n\n public class Solution {\n public int largestRectangleArea(int[] heigh
arkansol
NORMAL
2015-10-19T03:09:56+00:00
2015-10-19T03:09:56+00:00
1,445
false
It passes the custom testcase in 24ms, but when I submit it's TLE..\n\nCode:\n\n public class Solution {\n public int largestRectangleArea(int[] height) {\n Stack<Integer> maxIdx = new Stack<Integer>();\n int area = 0;\n for (int i = 0; i < height.length; i++) {\n ...
11
0
[]
2
largest-rectangle-in-histogram
Java Easy Solution
java-easy-solution-by-suryanshhbtu-1pko
\nclass Solution {\n public int largestRectangleArea(int[] heights) {\n int ans=0;\n int ps[]=previousSmall(heights);\n int ns[]=nextSma
suryanshhbtu
NORMAL
2022-02-05T19:20:22.559218+00:00
2022-02-05T19:20:22.559258+00:00
949
false
```\nclass Solution {\n public int largestRectangleArea(int[] heights) {\n int ans=0;\n int ps[]=previousSmall(heights);\n int ns[]=nextSmall(heights);\n for(int i=0 ;i<heights.length ;i++){\n ans=Math.max(ans,(ns[i]-ps[i]-1)*heights[i]);\n }\n return ans;\n }\...
10
0
['Stack', 'Java']
0
largest-rectangle-in-histogram
Easy C++ sol with just one traversal
easy-c-sol-with-just-one-traversal-by-by-cazx
Pre-requisite = Previous smaller element using stack.\n\nIdea :-The idea is to use the concept of next smaller element and previous smaller element, we will mai
byteZorvin
NORMAL
2022-01-29T07:58:51.097926+00:00
2022-01-29T09:22:58.099945+00:00
301
false
**Pre-requisite** = Previous smaller element using stack.\n\n**Idea** :-The idea is to use the concept of next smaller element and previous smaller element, we will maintain the index of previous smaller element in the stack and we can get the next smaller element when we pop the element from the stack as the element i...
10
0
[]
1
largest-rectangle-in-histogram
python using stack
python-using-stack-by-texasroh-0fcz
\nclass Solution:\n def largestRectangleArea(self, heights: List[int]) -> int:\n ans = 0\n stack = []\n heights = [0] + heights + [0]\n
texasroh
NORMAL
2020-02-16T06:44:15.079848+00:00
2020-02-16T06:44:15.079884+00:00
1,729
false
```\nclass Solution:\n def largestRectangleArea(self, heights: List[int]) -> int:\n ans = 0\n stack = []\n heights = [0] + heights + [0]\n for i in range(len(heights)):\n while(stack and heights[stack[-1]] > heights[i]):\n j = stack.pop()\n ans = m...
10
0
['Stack', 'Python']
2
largest-rectangle-in-histogram
16ms [c++] 10-line code with stack
16ms-c-10-line-code-with-stack-by-cq2159-e18b
int largestRectangleArea(vector<int>& height) {\n height.push_back(0); \n int len = height.size(),res = 0, cur=1;\n int s[len+1]={0};\n
cq2159
NORMAL
2015-10-03T20:35:45+00:00
2015-10-03T20:35:45+00:00
3,221
false
int largestRectangleArea(vector<int>& height) {\n height.push_back(0); \n int len = height.size(),res = 0, cur=1;\n int s[len+1]={0};\n s[0]=-1;\n for(int i=1;i<len;i++){\n while(cur && height[i]<height[s[cur]])\n res = max(res, height[s[cur]] * (i-s[--cu...
10
1
['C++']
3
largest-rectangle-in-histogram
10 line c++ solution
10-line-c-solution-by-kenigma-wbkg
\n\n class Solution {\n public:\n int largestRectangleArea(vector& heights) {\n int res = 0;\n for (int i = 0; i < heights.si
kenigma
NORMAL
2016-03-03T18:10:05+00:00
2018-09-12T13:20:56.131499+00:00
2,515
false
\n\n class Solution {\n public:\n int largestRectangleArea(vector<int>& heights) {\n int res = 0;\n for (int i = 0; i < heights.size(); ++i) {\n if (i + 1 < heights.size() && heights[i] <= heights[i+1]) continue; // find the local max (greater than left and right)\n ...
10
1
['C++']
3
largest-rectangle-in-histogram
✅ Simple clean stack approach with explanation
simple-clean-stack-approach-with-explana-0mx2
Approach\n1. Monotonic Stack: Utilize a monotonic stack to keep track of heights and indices in ascending order. If the height encountered is less than the heig
MostafaAE
NORMAL
2024-03-24T03:16:47.119682+00:00
2024-03-24T03:16:47.119710+00:00
1,185
false
# Approach\n1. Monotonic Stack: Utilize a monotonic stack to keep track of heights and indices in ascending order. If the height encountered is less than the height at the top of the stack, pop elements from the stack and calculate the area of the rectangles until the current index.\n2. Compute Largest Area: While popp...
9
0
['Stack', 'Monotonic Stack', 'C++']
1
largest-rectangle-in-histogram
Easy understanding solution with monotonic stack C++ (image explanation included)
easy-understanding-solution-with-monoton-zk9g
Intuition\n Describe your first thoughts on how to solve this problem. \nTry to find the longest width w.r.t. height[i], which means we have to find the most le
sunskyxh
NORMAL
2023-12-21T01:28:30.861537+00:00
2023-12-21T01:30:30.396734+00:00
663
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTry to find the longest width w.r.t. `height[i]`, which means we have to find the most left (called `start`) and right (called `end`) positions where the all the heights between `[start, end]` are greater or equal to `height[i]`.\n\n# App...
9
0
['Greedy', 'Monotonic Stack', 'C++']
0
largest-rectangle-in-histogram
Simplest C# solution with stack<int>
simplest-c-solution-with-stackint-by-sto-brey
\n\npublic class Solution {\n public int LargestRectangleArea(int[] heights) {\n int n = heights.Length;\n int max = 0;\n var stack = ne
stodorov
NORMAL
2023-09-23T12:04:13.772142+00:00
2023-09-23T12:04:13.772161+00:00
591
false
\n```\npublic class Solution {\n public int LargestRectangleArea(int[] heights) {\n int n = heights.Length;\n int max = 0;\n var stack = new Stack<int>();\n\n for(int i = 0; i <= n; i++)\n {\n var height = i < n ? heights[i] : 0;\n\n while(stack.Any() && heigh...
9
0
['C#']
1
largest-rectangle-in-histogram
C++ || Stack Solution || Next smaller to left &Right
c-stack-solution-next-smaller-to-left-ri-2bi0
\t// find the index vector for next smaller to left\n\t// find the index vector for next smaller to right \n\t// width will be right[i]-left[i]-1 \n\t// here st
anubhavsingh11
NORMAL
2022-06-24T17:55:28.808295+00:00
2022-06-24T17:55:28.808340+00:00
465
false
\t// find the index vector for next smaller to left\n\t// find the index vector for next smaller to right \n\t// width will be right[i]-left[i]-1 \n\t// here stack<pair<int,int>> first element in pair denotes height[i] and \n\t// second element of pair corresponds to index i \n\n\tclass Solution {\n\tpublic:\n\t\t// ne...
9
0
['Stack', 'C']
1
largest-rectangle-in-histogram
JavaScript Using Stack Beat 97%
javascript-using-stack-beat-97-by-contro-3zwu
Time Complexity = O(N)\nSpace Complexity = O(N)\njavascript\nvar largestRectangleArea = function(heights) {\n // to deal with last element without going out
control_the_narrative
NORMAL
2020-07-16T15:57:07.544484+00:00
2020-07-16T18:15:11.288860+00:00
1,562
false
Time Complexity = O(N)\nSpace Complexity = O(N)\n```javascript\nvar largestRectangleArea = function(heights) {\n // to deal with last element without going out of bound\n heights.push(0)\n const stack = [];\n let maxArea = 0, curr, currH, top, topH, area;\n \n for(let i = 0; i < heights.length; i++) {...
9
1
['JavaScript']
2
largest-rectangle-in-histogram
Short C++ solution use stack AC 28ms
short-c-solution-use-stack-ac-28ms-by-cc-lb52
class Solution {\n public:\n int largestRectangleArea(vector<int>& height) {\n height.push_back(0);\n int result=0;\n
ccweikui
NORMAL
2015-06-06T04:45:44+00:00
2018-08-15T22:53:27.420024+00:00
2,362
false
class Solution {\n public:\n int largestRectangleArea(vector<int>& height) {\n height.push_back(0);\n int result=0;\n stack<int> indexStack;\n for(int i=0;i<height.size();i++){\n while(!indexStack.empty()&&height[i]<height[indexStack.top()]){\n ...
9
0
[]
1
largest-rectangle-in-histogram
Explination of the stack solution (python solution)
explination-of-the-stack-solution-python-t8qk
To get the bigest rectangle area, we should check every rectangle with lowest point at i=1...n. \n\nDefine Si := the square with lowest point at i. \nTo calcu
jddymx
NORMAL
2015-10-09T20:40:21+00:00
2015-10-09T20:40:21+00:00
2,804
false
To get the bigest rectangle area, we should check every rectangle with lowest point at i=1...n. \n\nDefine `Si := the square with lowest point at i`. \nTo calculate faster this `Si` , we have to use a stack `stk` which stores some *indices*. \n\nThe elements in stk satisfy these **properties**: \n\n1. the indices a...
9
0
['Python']
1
largest-rectangle-in-histogram
Python solution with detailed explanation
python-solution-with-detailed-explanatio-b89x
Solution with discussion https://discuss.leetcode.com/topic/77574/python-solution-with-detailed-explanation\n\nLargest Rectangle in Histogram https://leetcode.c
gabbu
NORMAL
2017-02-02T17:34:57.476000+00:00
2018-08-27T02:01:09.537966+00:00
2,204
false
**Solution with discussion** https://discuss.leetcode.com/topic/77574/python-solution-with-detailed-explanation\n\n**Largest Rectangle in Histogram** https://leetcode.com/problems/largest-rectangle-in-histogram/\n\nhttp://www.geeksforgeeks.org/largest-rectangle-under-histogram/\nhttp://www.geeksforgeeks.org/largest-rec...
9
1
[]
3
largest-rectangle-in-histogram
My 16ms C++ O(n) code without a stack
my-16ms-c-on-code-without-a-stack-by-bea-fqrm
Basically, I find the range (left[i], right[i]) within which the height of every element is at least height[i]. For each i, there is a rectangular with area hei
bear2015
NORMAL
2015-09-05T16:35:55+00:00
2015-09-05T16:35:55+00:00
2,534
false
Basically, I find the range (left[i], right[i]) within which the height of every element is at least height[i]. For each i, there is a rectangular with area height[i] * (right[i]-left[i]+1), and the largest area must be one of them. Then run all i to find the largest area. Please notice it takes O(n) time to fill left[...
9
0
['Dynamic Programming', 'C++']
2
largest-rectangle-in-histogram
TWO APPROACHES USING LEFT AND RIGHT MINIMUM || SINGLE PASS || O( N ) ✅✅
two-approaches-using-left-and-right-mini-n6ad
Approach 1\n\n### Intuition:\nWe can solve this problem using a stack to efficiently calculate the left and right boundaries of each bar.\n\n### Approach:\n1. U
Sabari_Raj_004
NORMAL
2024-05-07T06:36:31.386832+00:00
2024-05-07T06:36:31.386865+00:00
1,004
false
## Approach 1\n\n### Intuition:\nWe can solve this problem using a stack to efficiently calculate the left and right boundaries of each bar.\n\n### Approach:\n1. Use two passes:\n - In the first pass, calculate the left boundary of each bar.\n - In the second pass, calculate the right boundary of each bar and simul...
8
0
['Array', 'Stack', 'Monotonic Stack', 'Java']
1
largest-rectangle-in-histogram
✅Easy✨||C++ || Beats 100% || With proper Explanation
easyc-beats-100-with-proper-explanation-g5zhb
Intuition\n Describe your first thoughts on how to solve this problem. \nWe will use two function next smaller and prev smaller element function and by getting
olakade33
NORMAL
2024-02-22T14:08:02.718362+00:00
2024-02-22T14:08:02.718396+00:00
1,199
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe will use two function next smaller and prev smaller element function and by getting that value we will find width and length is properly the height of histogram by simply just multiplying width and length we will get area and at the en...
8
0
['C++']
0
largest-rectangle-in-histogram
Shortest Code || Rat bhi skte ho
shortest-code-rat-bhi-skte-ho-by-surajma-l0vc
daalo -1 stack mein, aur 0 ko nums ke back mein\n2. jaldi se krlo pura yaad, interviewer ke saamne krdena anuvaad\n3. upvote kroge to milegi jrur, ldki nhi jo
surajmamgai
NORMAL
2022-09-01T22:33:28.529643+00:00
2022-09-01T22:46:35.415497+00:00
424
false
1. **daalo -1 stack mein, aur 0 ko nums ke back mein**\n2. **jaldi se krlo pura yaad, interviewer ke saamne krdena anuvaad**\n3. **upvote kroge to milegi jrur, ldki nhi job huzur**\n\n\n\n\n```\n int largestRectangleArea(vector<int>& nums) {\n stack<int> st;\n st.push(-1);\n nums.push_back(0);\n int ans=0...
8
0
[]
1
largest-rectangle-in-histogram
Detailed explanation with diagrams and examples using pen and paper|| Brute-force and Optimized
detailed-explanation-with-diagrams-and-e-3tks
Explanation\n\n\n\n\n\n\n\nJAVA Code\n1. Brute force (To understand he concept)\n\nclass Solution {\n public int largestRectangleArea(int[] heights) {\n
rachana_raut
NORMAL
2022-01-29T17:03:19.883695+00:00
2022-01-29T17:03:19.883727+00:00
478
false
**Explanation**\n\n![image](https://assets.leetcode.com/users/images/2c86f2b6-821c-44d5-8ae4-0f016dfcdf61_1643475595.6156461.jpeg)\n\n![image](https://assets.leetcode.com/users/images/20acec8f-02f2-47ec-9c88-94b88577ad7f_1643475593.3028414.jpeg)\n\n\n\n**JAVA Code**\n1. Brute force (To understand he concept)\n```\nclas...
8
0
['Java']
1
largest-rectangle-in-histogram
Easy Brute Force approach | C++
easy-brute-force-approach-c-by-vaishnavi-p870
\nclass Solution {\npublic:\n int largestRectangleArea(vector<int>& heights) {\n int n=heights.size(),prev,next;\n long long maxi=0;\n f
vaishnavi_vv
NORMAL
2022-01-29T12:10:01.750753+00:00
2022-01-29T12:10:01.750798+00:00
384
false
```\nclass Solution {\npublic:\n int largestRectangleArea(vector<int>& heights) {\n int n=heights.size(),prev,next;\n long long maxi=0;\n for(int i=0;i<n;i++)\n {\n int c=1;\n prev=i-1;\n next=i+1;\n while(prev>=0 && heights[i]<=heights[prev]) /...
8
1
['C']
2
largest-rectangle-in-histogram
Python3 O(N log(N)) divide and conquer approach explained
python3-on-logn-divide-and-conquer-appro-34wq
The first approach I came up with was to try every possible pair of start and end positions, keeping track of the minimum encountered height since the correspon
alexeir
NORMAL
2020-04-04T10:59:16.952373+00:00
2020-04-04T14:58:34.490542+00:00
1,005
false
The first approach I came up with was to try every possible pair of start and end positions, keeping track of the minimum encountered height since the corresponding starting position and pick the pair achieving maximum area. This can be implemented in O(N^2) time complexity. This approach exceeded the time limit (at le...
8
0
['Divide and Conquer', 'Python3']
1