|
| 1 | +/** |
| 2 | + * 1033. Moving Stones Until Consecutive |
| 3 | + * https://leetcode.com/problems/moving-stones-until-consecutive/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * There are three stones in different positions on the X-axis. You are given three integers a, b, |
| 7 | + * and c, the positions of the stones. |
| 8 | + * |
| 9 | + * In one move, you pick up a stone at an endpoint (i.e., either the lowest or highest position |
| 10 | + * stone), and move it to an unoccupied position between those endpoints. Formally, let's say the |
| 11 | + * stones are currently at positions x, y, and z with x < y < z. You pick up the stone at either |
| 12 | + * position x or position z, and move that stone to an integer position k, with x < k < z and |
| 13 | + * k != y. |
| 14 | + * |
| 15 | + * The game ends when you cannot make any more moves (i.e., the stones are in three consecutive |
| 16 | + * positions). |
| 17 | + * |
| 18 | + * Return an integer array answer of length 2 where: |
| 19 | + * - answer[0] is the minimum number of moves you can play, and |
| 20 | + * - answer[1] is the maximum number of moves you can play. |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * @param {number} a |
| 25 | + * @param {number} b |
| 26 | + * @param {number} c |
| 27 | + * @return {number[]} |
| 28 | + */ |
| 29 | +var numMovesStones = function(a, b, c) { |
| 30 | + const positions = [a, b, c].sort((x, y) => x - y); |
| 31 | + const minGap = Math.min(positions[1] - positions[0], positions[2] - positions[1]); |
| 32 | + const totalGap = positions[2] - positions[0] - 2; |
| 33 | + |
| 34 | + const minMoves = positions[1] - positions[0] === 1 && positions[2] - positions[1] === 1 |
| 35 | + ? 0 : minGap <= 2 ? 1 : 2; |
| 36 | + const maxMoves = totalGap; |
| 37 | + |
| 38 | + return [minMoves, maxMoves]; |
| 39 | +}; |
0 commit comments