1
2
3
4
5
6
7
8
1
8
6
2
5
4
8
3
7
[0][1][2][3][4][5][6][7][8]
Brute force
▸1given heights[]2max ← 03for i ← 0 to n − 2:4 for j ← i + 1 to n − 1:5 area = (j − i) × min(h[i], h[j])6 if area > max: max ← area7return max
state
- n9
Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.
Try one problemGiven heights representing vertical lines, pick two lines that together with the x-axis form a container holding the most water, and return that maximum area.
▸1given heights[]2max ← 03for i ← 0 to n − 2:4 for j ← i + 1 to n − 1:5 area = (j − i) × min(h[i], h[j])6 if area > max: max ← area7return max
line 1We want two lines that, with the x-axis, hold the most water. area = (j − i) × min(h[i], h[j]).