I'm having issues implementing an optimal code to solve this problem. I do have some code I started with already but I'm stumped at a certain point. Any assistance you can offer would be appreciated.

Im having issues implementing an optimal code to solve this problem I do have some code I started with already but Im stumped at a certain point Any assistance class=