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

preview_player
Показать описание
**** Best Books For Data Structures & Algorithms for Interviews:**********
*****************************************************************************

Single Number ii (Leetcode)
Facebook Coding Interview - Single number ii

#Amazon #Facebook #CodingInterview #LeetCode #SingleNumber #Google
Рекомендации по теме
Комментарии
Автор

Nice explanation. we can Generalize as below.
int maxArea(vector<int>& height) {
int maxarea =0;
int minIndex = 0;
int maxIndex = height.size()-1;

while(minIndex!=maxIndex)
{

swap(minIndex, maxIndex);
int newarea = height[minIndex] * (abs(minIndex-maxIndex));
if(newarea>maxarea)
maxarea = newarea;

if(minIndex > maxIndex)
minIndex--;
else
minIndex++;
}
return maxarea;
}

AnandKumar-edyo
Автор

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.

avivfriedman
Автор

class Solution {
public:
int maxArea(vector<int>& height) {
int i = 0, j = height.size()-1;
int m = 0, m1;
while(i<j){
m1 = min(height[i], height[j]) * (j-i);
m = max(m, m1);
height[i]<height[j] ? i++ : j--;
}
return m;
}
};

ForCodingInterview
Автор

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.

ajnabee
Автор

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

Bad_Avadh
Автор

MY approach is way better btw bcuz we dont need to mutate the min height and idx
simple and better


int maxArea(vector<int>& height) {
// Write your code here.

int ans = 0, n = height.size();
int i = 0, j = n-1;
while(i<j){
if(height[i]>height[j]){
ans = max(ans, (j-i)*height[j]);
j--;
}
else{
ans = max(ans, (j-i)*height[i]);
i++;
}
}
return ans;

}

RajeevCanDev
Автор

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

heyrmi
Автор

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

sayantaniguha
Автор

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.

TheMsnitish
Автор

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

kolanaresh
Автор

int maxArea(vector<int>& height)
{
int n=height.size();
int i=0, j=n-1;
int area1=0;
int area;
int width;
int hmin;
while(i<j)
{
width=j-i;
if(height[i]>height[j])
{
hmin=height[j];
j--;
}
else
{
hmin=height[i];
i++;
}
area=width*hmin;
if(area>area1)
{
area1=area;
}
}
return area1;
}

kolanaresh
Автор

class Solution {
public:
int maxArea(vector<int>& height) {
int left=0;
int right=height.size()-1;
int max_area=0;
while(left<right){
int current_area=(right-left)*min(height[left], height[right]);
max_area=max(current_area, max_area);

left++;
}
else{
right--;
}
}
return max_area;
}
};

VishnuManojkumar-if
Автор

question ka link to provide kara dena chaiye

AMITVERMA-pr
Автор

code quality is not good
simple solution

class Solution {
public:
int maxArea(vector<int>& 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;
}
};

TrackToPune
Автор

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.

Автор

Bhai do you also plan to make videos in Hindi ?

TheMsnitish
Автор

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

tusharnain
Автор

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 .

damudaran
Автор

is that a correct answer ?

#include <iostream>
#include<valarray>
#include <cmath>
using namespace std;
int sum;
unsigned int maxArea;
int area;
int i, j;
int minArea = 213213;
int main() {
maxArea > 0;
area > 0;
valarray<int>a(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 << endl;
if (maxArea < area) {
= area;
}
if (minArea > area) {
= area;
}
}
else if (a[i] < a[i + j]) {
area = j * a[i];
if (maxArea < area) {
= area;
}
if (minArea > area) {
= area;
}
//cout << area << endl;
}
}

}
cout << "MaxArea=" << maxArea << endl;
cout << "MinArea=" << minArea << endl;

return 0;
}

burakgunes