Dec-02-2020, 11:31 PM
(This post was last modified: Dec-02-2020, 11:31 PM by deanhystad.)
Your solution is slow because the algorithm is inefficient. You are computing every possible volume (area) and picking the max. With 5000 heights this is 4999 * 4999 / 2 volumes to calculate and compare. When I run your solution it took 19 seconds.
Take some time and think about the problem instead of the code. There is a way to solve this problem with less than 5000 calculations and comparisons.
I'll give you three hints.
1. Solving for height[0], height[4999] is a good first guess because width is important.
2. The shorter "wall" limits the volume.
3. Draw a bar chart on paper with different sized walls when thinking about the problem.
I have a solution that runs in 0.0193 seconds and it isn't using any fancy Python to speed things up. Just an efficient algorithm.
Take some time and think about the problem instead of the code. There is a way to solve this problem with less than 5000 calculations and comparisons.
I'll give you three hints.
1. Solving for height[0], height[4999] is a good first guess because width is important.
2. The shorter "wall" limits the volume.
3. Draw a bar chart on paper with different sized walls when thinking about the problem.
I have a solution that runs in 0.0193 seconds and it isn't using any fancy Python to speed things up. Just an efficient algorithm.