Nice explanation. we can Generalize as below. int maxArea(vector& height) { int maxarea =0; int minIndex = 0; int maxIndex = height.size()-1; while(minIndex!=maxIndex) { if(height[minIndex]>height[maxIndex]) swap(minIndex,maxIndex); int newarea = height[minIndex] * (abs(minIndex-maxIndex)); if(newarea>maxarea) maxarea = newarea; if(minIndex > maxIndex) minIndex--; else minIndex++; } return maxarea; }
@avivfriedmanАй бұрын
If someone wants a short summary: The idea is to calculate the area between the lines at the two pointers, update the maximum area if the current area is larger, and then move the pointer pointing to the shorter line. By moving the shorter line, you potentially increase the height of the container, as increasing the width alone won't give a better result if one side is short.
@ForCodingInterview3 жыл бұрын
class Solution { public: int maxArea(vector& height) { int i = 0, j = height.size()-1; int m = 0,m1; while(i
@ajnabee013 жыл бұрын
One line sums up it all: If you have the max width between two bars, then the only way to increase the area is to increase the height. That is, the current area is the maximum for the area for the solution set containing the current minimum height bar.
@saivenkat91133 жыл бұрын
U made my bulb glow just with one line Thanks!
@Bad_Avadh2 жыл бұрын
better approaches int area; int maxarea=0; int start=0; int end=height.size()-1; while(start < end){ * we going to find the area of container* area=min(height[start], height[end])* (end-start); * after finding the area we check that if area is grater than maxarea we place the area value into maxarea* if(area > maxarea){ maxarea= area; } *after that we check that start value is less that end value than increment the start or else decrement the end index value* if(height [start] < height[end] ){ start++; } else { end--; } } return maxarea; please comment if any query is there?? thank you
@RajeevCanDev Жыл бұрын
MY approach is way better btw bcuz we dont need to mutate the min height and idx simple and better int maxArea(vector& height) { // Write your code here. int ans = 0, n = height.size(); int i = 0, j = n-1; while(iheight[j]){ ans = max(ans, (j-i)*height[j]); j--; } else{ ans = max(ans, (j-i)*height[i]); i++; } } return ans; }
@heyrmi3 жыл бұрын
I watch your videos for intuition. Then take time to code that logic.
@quant-prep28433 жыл бұрын
remember during the interview nobody will give you the intuititon, lol
@heyrmi3 жыл бұрын
@@quant-prep2843 working on it 😅
@VishnuManojkumar-i3f3 ай бұрын
class Solution { public: int maxArea(vector& height) { int left=0; int right=height.size()-1; int max_area=0; while(left
@kolanaresh70772 жыл бұрын
nice explanation,first problem i seen algorithm and solved my own.
@KnowledgeCenter2 жыл бұрын
Well done
@kolanaresh70772 жыл бұрын
int maxArea(vector& height) { int n=height.size(); int i=0,j=n-1; int area1=0; int area; int width; int hmin; while(iheight[j]) { hmin=height[j]; j--; } else { hmin=height[i]; i++; } area=width*hmin; if(area>area1) { area1=area; } } return area1; }
@poojaagarwal14804 жыл бұрын
Wow! Great Solution!
@KnowledgeCenter4 жыл бұрын
Glad you like it!
@ashishmadan57003 жыл бұрын
Amazing explanation !!
@KnowledgeCenter3 жыл бұрын
Thanks.
@TheMsnitish3 жыл бұрын
What kind of hardware you use for the pen ? Please share the amazon link if you have of the pen and digital slate you are using.
@KnowledgeCenter3 жыл бұрын
Hi. This is the pen. amzn.to/3tOVEP3
@TrackToPune2 жыл бұрын
code quality is not good simple solution class Solution { public: int maxArea(vector& height) { int maxArea = 0; int i = 0, j = height.size() - 1; while(i < j) { maxArea = max(maxArea, min(height[i], height[j]) * (j - i)); if(height[i] < height[j]) i++; else j--; } return maxArea; } };
@paragroy53594 жыл бұрын
Nice explanation sir....
@KnowledgeCenter4 жыл бұрын
Thanks for liking
@sayantaniguha85192 жыл бұрын
What is the difference b/w this Q and *Largest Rectangle in Histogram* ?
@quangxiang33824 жыл бұрын
Weiting fore premiere
4 жыл бұрын
Your explaination is good but your demo code is quite long. We just need to keep track left_height and right_height (with only 1 while loop)...anyway, thank you very much for your video.
@KnowledgeCenter4 жыл бұрын
Noted
@ramfattah2753 жыл бұрын
@@KnowledgeCenter this video is perfect 👍 as long as its not over 30 mins long🤣!! thanks for creating this C++ playlist
@TheMsnitish3 жыл бұрын
Bhai do you also plan to make videos in Hindi ?
@quant-prep28433 жыл бұрын
there is no bhai's,this is for all dont be a discriminant and gender based inequality
@TheMsnitish3 жыл бұрын
@@quant-prep2843 LMAO what's wrong with you ? Bhai doesnt mean what you have assumed to be. I hope you know that it is just a way to address someone politely,
@AMITVERMA-p5r Жыл бұрын
question ka link to provide kara dena chaiye
@tusharnain66522 жыл бұрын
This code is quite big, this can be done simply with only 1 while loop/
@damudaran37654 жыл бұрын
its a good explaination . but u are just telling the thing but u r not concernerd about the viewers understanding . be bold on your voice .
@aidanthompson5053 Жыл бұрын
4:32
@burakgunes25352 жыл бұрын
is that a correct answer ? #include #include #include using namespace std; int sum; unsigned int maxArea; int area; int i, j; int minArea = 213213; int main() { maxArea > 0; area > 0; valarraya(9); a = { 1,8,6,2,5,4,8,3,7 }; for (int i = 0; i < 9; i++) { for (int j = 1; i+j < 9; j++) { if (a[i] >= a[i + j]) { area = j * a[i + j]; //cout area) { minArea = area; } //cout