Simple Proof to the Container with Most Water Problem
leimao.github.io
leimao.github.io
“I was a Ph.D. candidate in the Department of Biochemistry at Duke University, working on DNA repair mechanisms. There, after spending almost four years, I systematically proved that most of the projects, theories and guidance from the advisor, which I was forced to do and obey, were wrong. To me, a sound Ph.D. degree or research position job title does not tell anything about the person’s capability of solving a problem and whether the person is a true scientist or not. I have already proved that I am better than most of the people there. In 2016, I decided to leave Duke University, with my own glory, without a Ph.D. degree.”
It just doesn't bother with the counting argument that OP makes.
Also, the largest cross section of a cube is a rectangle of size √2, not a hexagon (3/4 √3). The largest regular polygon cross section is a hexagon.
Start at i,j with heights h_i < h_j. Then we know that if i is involved in the solution, it must be paired with j.
We know this because if it's paired with any other point, the other point is either: a. shorter than h_i, which means it's shorter than h_j. And since it's also closer to i than j is, the area is smaller. b. equal or taller than h_i. But this will never make a larger area since the height will be limited by h_i, but the distance smaller than the distance from i to j.
So we can stop considering i as part of the solution space.
Of course that doesnt make it a good interview question. It relies on a brilliant stroke of insight, rather than relying on established data structures or algorithms.
The water container area is the distance the two sides are apart (width), by the height of the shorter side [therefore, the longer side acts to mimic it].
Doing brute force on array $[n_1 n_2 ... n_k]$;
You'd naturally start at $n_1$ and calculate volume $V(n_1, n_k)$ and work your way inwards calculating each volume. However, if at any point $n_1$ is the shorter side, then the secondary side $n_j$ now mimics $n_1$ in height; and therefore, any new side closer to $n_1$ cannot increase the height of the container (but would decrease the width, and therefore area).
Then you'd continue the same with $n_2$, breaking iteration when $n_2$ becomes the shorter side or if $n_2$ cannot develop a greater area than $n_1$ did (either n_1 > n_2 or oldArea > n_2 * width).
etc.
https://leetcode.com/problems/container-with-most-water/desc...
The way it’s written, the walls are uniformly spaced — located at their corresponding integers in the height array.
But the picture shows them as non-uniformly spaced: the wall to the right of the first red has a bigger separation than all the others.
Either "walls" are porous or they aren't.
7x7 > 8x5.
The starting point for this problem is provided as:
class Solution {
public:
int maxArea(vector<int>& height) {
}
};
I saw this problem in an interview about a decade ago, at one of the larger tech companies. Google or something. Supposedly Google doesn't do "algorithmic" interviews like this anymore.That's not true. In fact if anything that's all they do now.
That's not true. It is however, still a majority of what they do.
http://www.cplusplus.com/reference/iterator/RandomAccessIter...