Approach for solving problem

dynamic-programming
implementation

#1

Please read the query here
.