DP approach for HELP MARTHA

anyone who can provide a dynamic programming approach for the question:

I tried, but couldn’t get AC.
If you could share the approach or the code, I would be grateful

Why would you use DP for this problem?

1 Like

exactly, it can be solved in O(|S|), dp will take at least O(|S|^2).

1 Like

This is not a DP Problem. It can be easily solved by finding the extreme x and extreme y values that can be reached.