FastPrepCookie Monster Minimum Arrival Time

Cookie Monster Minimum Arrival Time

Intuit logoIntuit● MediumINTERNOA

Problem statement

A Cookie Monster starts in the upper-left room of a nonempty rectangular grid rooms. You are in its lower-right room. The start is marked M, your room is marked Y, a period marks an empty room, and any other uppercase letter marks a cookie type.

The monster may move one room up, down, left, or right, including moves up or left when they help. It cannot move diagonally. Every room is passable. The time to enter a room is:

  • 1 time unit for an empty room, M, or Y;
  • 2 time units for a cookie type absent from weaknesses;
  • 5 time units for a cookie type appearing in weaknesses.

The monster knows the entire grid and chooses the fastest route. Return its minimum total time to reach Y. It is already in M at time zero, so do not charge for the initial room.

Weaknesses is a nonempty string of uppercase letters excluding M and Y. A weakness need not appear in the grid. For this exercise, assume repeated weakness letters have the same meaning as one copy.

Function

cookieMonsterTime(rooms: String[], weaknesses: String) → int

Examples

Example 1

rooms = ["MDC.C","..CFF","GGGBE","EG.G.","EGDEY"]weaknesses = "CG"return = 15

A fastest route is (0,0) → (1,0) → (1,1) → (1,2) → (1,3) → (1,4) → (2,4) → (3,4) → (4,4). Its entered-room costs are 1, 1, 5, 2, 2, 2, 1, and 1, totaling 15. The initial M has no charge.

Example 2

rooms = ["MDC.C","..CFF","GGGBE","EG.G.","EGDEY"]weaknesses = "BCD"return = 11

A fastest route is (0,0) → (1,0) → (1,1) → (2,1) → (2,2) → (3,2) → (3,3) → (3,4) → (4,4). Its entered-room costs are 1, 1, 2, 2, 1, 2, 1, and 1, totaling 11. G costs 2 because it is not one of the weaknesses B, C, or D.

Constraints

  • For this exercise, assume 1 <= rooms.length <= 30, and every row has the same length from 1 to 30.
  • Each room is a period or an uppercase ASCII letter. M appears exactly once at the upper-left corner, and Y appears exactly once at the lower-right corner. The grid therefore contains at least two rooms.
  • For this exercise, assume 1 <= weaknesses.length <= 24. Every weakness is an uppercase ASCII letter other than M or Y.

More Intuit problems

See Intuit hiring insights
Saved
public int cookieMonsterTime(String[] rooms, String weaknesses) {
    // Write your code here
}
rooms["MDC.C","..CFF","GGGBE","EG.G.","EGDEY"]
weaknesses"CG"
expected15
Checking account…