Compass Interview Question

Dynamic programming algorithm problem