I tried solving one problem which was claimed to be solvable using Dynamic programming only. Following is the problem. A man has total energy as H and he needs to cover distance D. He wants to cover this distance using maximum energy upto H in minimum time. He can run in 5 modes. Total distance is covered by running each km according to one of the 5 modes. These five modes are described using two arrays which are sorted
Time:-'5m 10sec', '6 m 11sec','7 m 7sec','8 m 11sec','9 m 11sec'
Energy required:-11,9,8,7,6
So my greedy solution strategy is
- Compute x=floor of H/D
Run next km using mode which takes lowest time and requires energy<=x
& set H=H-energy required by mode
set D=D-1
Goto step 1 till remaining distance becomes 0.
- Answer will be some of all times for each km.
I think this solution will work but it fails in reality. I know how to solve it using DP, but I wanted to know where it fails. I have tried many examples but not getting the feeling. I do not recall above two arrays elements exactly but they are necessarily in sorted order.