Burning candle problem

By | December 20, 2014
Share the joy
  •  
  •  
  •  
  •  
  •  
  •  

Problem description:
You are given n candles with size of each candle present as an element in an integer array i.e a[i] represent length of ith candle. There is a tradition according to which you celebrate by lighting a candle every night. The candles are lit such that there are k candles lit on the kth day. For ex. 1 candle on day 1 , 2 candles on day 2 and so on. Each night a candle burns 1 inch.
you need to write a program to find out maximum number of days you can celebrate the tradition with the n candles and their lengths?

In my opinon, it is a greedy algorithms. The trick of this quesiton, is that you burn those longest candle for each day. Until one day, you can’t find enough candles to burn. Use a descend linked-list to store the candle. Each time, select the candles from the beginning of the array.