A careless worker planted several poles in a row to build a fence.
They all should be the same height, but they were cut at different sizes.
The owner wants them not only all at the same height, but also as tall as possible.
Our solution will be to cut the tops of taller poles and "glue" those tops on top of the shorter ones.
To do this, we will first sort the poles from tallest to shortest, and proceed as follows:
- Cut the tip of the tallest pole, leaving its height equal to the average height of the poles (so we do not cut it anymore).
- "glue" this piece on top of the shortest pole.
- Re-sort the poles, and continue from the first step until all poles are the same height.
Write a class CuttingPoles with a method countCuts that takes a int[]
of pole heights and returns the number of cuts needed to make them all the same height using
the algorithm described.
|