LeetCode #11: Container with most Water | Facebook Interview Question | C++

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ต.ค. 2024

ความคิดเห็น • 39

  • @avivfriedman
    @avivfriedman 6 วันที่ผ่านมา +2

    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.

  • @AnandKumar-ed3yo
    @AnandKumar-ed3yo 4 ปีที่แล้ว +12

    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;
    }

  • @ForCodingInterview
    @ForCodingInterview 3 ปีที่แล้ว +9

    class Solution {
    public:
    int maxArea(vector& height) {
    int i = 0, j = height.size()-1;
    int m = 0,m1;
    while(i

  • @ajnabee01
    @ajnabee01 3 ปีที่แล้ว +7

    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.

    • @saivenkat9113
      @saivenkat9113 3 ปีที่แล้ว +1

      U made my bulb glow just with one line Thanks!

  • @Bad_Avadh
    @Bad_Avadh 2 ปีที่แล้ว +4

    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

  • @kolanaresh7077
    @kolanaresh7077 2 ปีที่แล้ว +1

    nice explanation,first problem i seen algorithm and solved my own.

  • @heyrmi
    @heyrmi 3 ปีที่แล้ว +2

    I watch your videos for intuition. Then take time to code that logic.

    • @quant-prep2843
      @quant-prep2843 3 ปีที่แล้ว

      remember during the interview nobody will give you the intuititon, lol

    • @heyrmi
      @heyrmi 3 ปีที่แล้ว

      @@quant-prep2843 working on it 😅

  • @RajeevCanDev
    @RajeevCanDev ปีที่แล้ว +1

    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;
    }

  • @VishnuManojkumar-i3f
    @VishnuManojkumar-i3f 2 หลายเดือนก่อน

    class Solution {
    public:
    int maxArea(vector& height) {
    int left=0;
    int right=height.size()-1;
    int max_area=0;
    while(left

  • @TheMsnitish
    @TheMsnitish 3 ปีที่แล้ว +2

    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.

    • @KnowledgeCenter
      @KnowledgeCenter  3 ปีที่แล้ว

      Hi. This is the pen. amzn.to/3tOVEP3

  • @kolanaresh7077
    @kolanaresh7077 2 ปีที่แล้ว

    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;
    }

  • @TrackToPune
    @TrackToPune 2 ปีที่แล้ว +2

    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;
    }
    };

  • @TheMsnitish
    @TheMsnitish 3 ปีที่แล้ว +2

    Bhai do you also plan to make videos in Hindi ?

    • @quant-prep2843
      @quant-prep2843 3 ปีที่แล้ว

      there is no bhai's,this is for all dont be a discriminant and gender based inequality

    • @TheMsnitish
      @TheMsnitish 3 ปีที่แล้ว +2

      @@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,

  • @ashishmadan5700
    @ashishmadan5700 3 ปีที่แล้ว +1

    Amazing explanation !!

  • @poojaagarwal1480
    @poojaagarwal1480 4 ปีที่แล้ว +1

    Wow! Great Solution!

  • @sayantaniguha8519
    @sayantaniguha8519 2 ปีที่แล้ว +1

    What is the difference b/w this Q and *Largest Rectangle in Histogram* ?

  • @paragroy5359
    @paragroy5359 4 ปีที่แล้ว +1

    Nice explanation sir....

  •  4 ปีที่แล้ว +1

    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.

    • @KnowledgeCenter
      @KnowledgeCenter  4 ปีที่แล้ว +1

      Noted

    • @ramfattah275
      @ramfattah275 3 ปีที่แล้ว

      @@KnowledgeCenter this video is perfect 👍 as long as its not over 30 mins long🤣!! thanks for creating this C++ playlist

  • @damudaran3765
    @damudaran3765 4 ปีที่แล้ว +8

    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 .

  • @quangxiang3382
    @quangxiang3382 4 ปีที่แล้ว +2

    Weiting fore premiere

  • @AMITVERMA-p5r
    @AMITVERMA-p5r ปีที่แล้ว +1

    question ka link to provide kara dena chaiye

  • @burakgunes2535
    @burakgunes2535 ปีที่แล้ว

    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

  • @tusharnain6652
    @tusharnain6652 2 ปีที่แล้ว

    This code is quite big, this can be done simply with only 1 while loop/

  • @aidanthompson5053
    @aidanthompson5053 ปีที่แล้ว

    4:32