classSolution constval FORWARD = 0 constval BACK = 1 constval MIN_POS = 0 constval MAX_POS = 6000 fun Solution.minimumJumps(forbidden: IntArray, a: Int, b: Int, x: Int): Int { val bfs = ArrayDeque<Pair<Int, Int>>() val skip = HashSet<Pair<Int, Int>>()
forbidden.forEach { skip.add(FORWARD to it);skip.add(BACK to it) } bfs.addLast(FORWARD to 0)
var step = 0 while (bfs.isNotEmpty()) { var cnt = bfs.size while (cnt-- > 0) { val pair = bfs.removeFirst() val direction = pair.first val position = pair.second if (position == x) { return step } if (position + a <= MAX_POS && !skip.contains(FORWARD to position + a)) { skip.add(FORWARD to position + a) bfs.addLast(FORWARD to position + a) } if (position - b >= MIN_POS && !skip.contains(BACK to position - b) && direction != BACK) { skip.add(BACK to position - b) bfs.addLast(BACK to position - b) } } step++ } return -1 }