How do you cut the sticks in HackerRank?
This tutorial provides Java solution to “Cut the sticks” challenge of HackerRank. You are given N sticks, where the length of each stick is a positive integer. A cut operation is performed on the sticks such that all of them are reduced by the length of the smallest stick.
What’s the best way to cut a stick?
Suppose we have six sticks of the following lengths: Then, in one cut operation we make a cut of length 2 from each of the six sticks. For the next cut operation four sticks are left (of non-zero length), whose lengths are the following: The above step is repeated until no sticks are left.
How do you cut the sticks in RuneScape?
You are given a number of sticks of varying lengths. You will iteratively cut the sticks into smaller sticks, discarding the shortest pieces until there are none left.
What’s the problem with cutting the sticks in Java?
The problem with that is that your list will have to shift all the elements one position towards the beginning of the list: so, if you have n sticks, taking out the first will take Θ ( n) time. Instead, you could sort the sticks into descending order and keep removing the last stick (which is Θ ( 1) per stick).
You will iteratively cut the sticks into smaller sticks, discarding the shortest pieces until there are none left. At each iteration you will determine the length of the shortest stick remaining, cut that length from each of the longer sticks and then discard all the pieces of that shortest length.
At each iteration you will determine the length of the shortest stick remaining, cut that length from each of the longer sticks and then discard all the pieces of that shortest length. When all the remaining sticks are the same length, they cannot be shortened so discard them.
How to calculate the number of sticks to cut?
The number of sticks at each iteration are . Complete the cutTheSticks function in the editor below. It should return an array of integers representing the number of sticks before each cut operation is performed. cutTheSticks has the following parameter (s):