Amazon | Online Round | USA
Anonymous User
855

I got asked the Dungeon Game (Leetcode #174) question, with everything almost the same, but with 2 additional criteria -

(Instead of integers in a 2D grid, we have strings of integers, e.g. "-5", "289", etc.)

Along with strings of integers, there are 2 other values that can be in a box - "P" or "D"

  1. If a box contains "P", the next time we step onto a box with a negative integer string, it's effect gets cancelled.

NOTE - stepping on multiple "P" boxes consecutively doesn't stack the effects, that is, if I walk into 2 "P" boxes and then 2 boxes with negative integer strings, I take damage from the second negative integer box.

E.g. If I step onto "P", then encounter "-5", instead of my score becoming score -5, my score becomes score + 0

  1. If a box contains "D", the next time we step onto a box with a positive integer string, its effect gets doubled.

NOTE - stepping on multiple "D" boxes consecutively doesn't stack the effects, that is, if I walk into 2 "D" boxes, then onto 2 boxes with positive integer strings X and Y, my score at the end would be ==> score + 2X + Y, and NOT score + 2X + 2Y.

E.g. if I step onto "D", then encounter "4", instead of my score becoming score + 4, it will become score + 8.

I was able to solve the question correctly without the P or D part.
(I had an idea to solve this part using if-else, but it was getting too complex).

Looking for suggestions on solving this problem.

Comments (2)