FastPrepDistinct Positions After One Teleporter Board Roll

Distinct Positions After One Teleporter Board Roll

Atlassian logoAtlassian● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

A board has squares numbered from 0 through lastNumber. Starting at startPosition, roll a die exactly once. Each possible roll is an integer from 1 through maxValue.

A roll first moves forward by its value. If it would pass lastNumber, land on lastNumber instead. Each string in teleporters has the form source,destination.

For this exercise, assume a landing applies the teleporter at that square exactly once. A destination does not trigger another teleporter, and the starting square does not teleport before the roll. Teleporter sources are unique, all squares are valid, and the final square is not a teleporter source.

Return all distinct final positions obtainable after this one roll, in increasing order. For this exercise, repeated positions appear once, including when several rolls clamp to the final square. Do not modify the input.

Function

oneRollLandings(teleporters: String[], maxValue: int, startPosition: int, lastNumber: int) → int[]

Examples

Example 1

teleporters = ["3,1","5,10","8,2"]maxValue = 6startPosition = 0lastNumber = 10return = [1,2,4,6,10]

The three teleporter strings come from the report; the die, start and board limit are authored. Rolls 1 through 6 finish at 1, 2, 1, 4, 10, and 6.

Example 2

teleporters = []maxValue = 6startPosition = 8lastNumber = 10return = [9,10]

Roll 1 reaches 9. Roll 2 reaches 10 exactly; larger rolls clamp to 10.

Example 3

teleporters = ["1,4","4,2"]maxValue = 1startPosition = 0lastNumber = 5return = [4]

The landing at 1 moves to 4 once. The teleporter at 4 is not followed during this turn.

Constraints

  • For this exercise, assume 1 <= lastNumber <= 1000, 0 <= startPosition <= lastNumber, and 1 <= maxValue <= lastNumber + 1.
  • 0 <= teleporters.length <= lastNumber.
  • Each string contains two nonnegative integers in canonical ASCII decimal notation, separated by one comma, with no spaces or signs and no leading zeros except for 0. Its unique source lies in [0,lastNumber-1] and destination lies in [0,lastNumber].

More Atlassian problems

See Atlassian hiring insights
public int[] oneRollLandings(String[] teleporters, int maxValue, int startPosition, int lastNumber) {
    // Write your code here.
    return new int[0];
}
teleporters["3,1","5,10","8,2"]
maxValue6
startPosition0
lastNumber10
expected[1,2,4,6,10]
Checking account…