Find minimum trees to cut so that we will be getting at least k units of wood
Given array contains heights of trees in ascending order
2 5 7 8 11 13 17
If we cut a tree of height x then all the trees above height x will be cut and will add to the total units of wood.So for each tree of height say y we will get y-x units of wood.We need to find minimum number of trees to cut to make k units of wood
Strategy:
In the given array lets say we want to make 18 units
We will start from 13.So sum=17-13=4 units only <18
So we move left to 11 and find again sum as sum= previuos sum+ difference between current & next element * number of elements after curent element
So sum= 4+ (13-11) * 2=8 units <18
Again we move to 8 & find sum as sum= 8+ (11-8) * 3 [ as 11,13,17 --3 elements are there towards right]
=17 <18
So we move to 7 and sum= 17 + 1*4=21>18 So 7 is my answer
Given array contains heights of trees in ascending order
2 5 7 8 11 13 17
If we cut a tree of height x then all the trees above height x will be cut and will add to the total units of wood.So for each tree of height say y we will get y-x units of wood.We need to find minimum number of trees to cut to make k units of wood
Strategy:
In the given array lets say we want to make 18 units
We will start from 13.So sum=17-13=4 units only <18
So we move left to 11 and find again sum as sum= previuos sum+ difference between current & next element * number of elements after curent element
So sum= 4+ (13-11) * 2=8 units <18
Again we move to 8 & find sum as sum= 8+ (11-8) * 3 [ as 11,13,17 --3 elements are there towards right]
=17 <18
So we move to 7 and sum= 17 + 1*4=21>18 So 7 is my answer
No comments:
Post a Comment