Skip to content

Commit 29cde00

Browse files
committed
Add solution #1033
1 parent 198ad27 commit 29cde00

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,104 LeetCode solutions in JavaScript
1+
# 1,105 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -840,6 +840,7 @@
840840
1030|[Matrix Cells in Distance Order](./solutions/1030-matrix-cells-in-distance-order.js)|Easy|
841841
1031|[Maximum Sum of Two Non-Overlapping Subarrays](./solutions/1031-maximum-sum-of-two-non-overlapping-subarrays.js)|Medium|
842842
1032|[Stream of Characters](./solutions/1032-stream-of-characters.js)|Hard|
843+
1033|[Moving Stones Until Consecutive](./solutions/1033-moving-stones-until-consecutive.js)|Medium|
843844
1037|[Valid Boomerang](./solutions/1037-valid-boomerang.js)|Easy|
844845
1038|[Binary Search Tree to Greater Sum Tree](./solutions/1038-binary-search-tree-to-greater-sum-tree.js)|Medium|
845846
1041|[Robot Bounded In Circle](./solutions/1041-robot-bounded-in-circle.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

Comments
 (0)