Algorithm

Problem Name: 1040. Moving Stones Until Consecutive II

There are some stones in different positions on the X-axis. You are given an integer array stones, the positions of the stones.

Call a stone an endpoint stone if it has the smallest or largest position. In one move, you pick up an endpoint stone and move it to an unoccupied position so that it is no longer an endpoint stone.

• In particular, if the stones are at say, stones = [1,2,5], you cannot move the endpoint stone at position 5, since moving it to any position (such as 0, or 3) will still keep that stone as an endpoint stone.

The game ends when you cannot make any more moves (i.e., the stones are in three consecutive positions).

Return an integer array answer of length 2 where:

• answer[0] is the minimum number of moves you can play, and
• answer[1] is the maximum number of moves you can play.

Example 1:

Input: stones = [7,4,9]
Output: [1,2]
Explanation: We can move 4 -> 8 for one move to finish the game.
Or, we can move 9 -> 5, 4 -> 6 for two moves to finish the game.

Example 2:

Input: stones = [6,5,4,3,10]
Output: [2,3]
Explanation: We can move 3 -> 8 then 10 -> 7 to finish the game.
Or, we can move 3 -> 7, 4 -> 8, 5 -> 9 to finish the game.
Notice we cannot move 10 -> 2 to finish the game, because that would be an illegal move.

Constraints:

• 3 <= stones.length <= 104
• 1 <= stones[i] <= 109
• All the values of stones are unique.

Code Examples

#1 Code Example with Javascript Programming

Code - Javascript Programming

const numMovesStonesII = function(stones) {
stones.sort((a, b) => a - b)
let n = stones.length;
let least = Number.MAX_VALUE, most = Number.MIN_VALUE;

for (let i = 0, j = 0; i  <  n; i++) {
while (j + 1 < n && stones[j + 1] - stones[i] < n) j++;
let now = n - (j - i + 1);
if (j - i == n - 2 && stones[j] - stones[i] == j - i) now++;
least = Math.min(least, now);
}

most = Math.max(stones[n - 1] - stones[1], stones[n - 2] - stones[0]) - (n - 2);
return [least, most];
};
Copy The Code &

Input

cmd
stones = [7,4,9]

Output

cmd
[1,2]

#2 Code Example with Python Programming

Code - Python Programming

List[int]:
A.sort()
i, n, low = 0, len(A), len(A)
high = max(A[-1] - n + 2 - A[1], A[-2] - A[0] - n + 2)
for j in range(n):
while A[j] - A[i] >= n: i += 1
if j - i + 1 == n - 1 and A[j] - A[i] == n - 2:
low = min(low, 2)
else:
low = min(low, n - (j - i + 1))
return [low, high]
Copy The Code &

Input

cmd
stones = [7,4,9]

Output

cmd
[1,2]
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://devsenv.com/example/-1040-leetcode-moving-stones-until-consecutive-ii-solution-in-c,-c++,-java,-javascript,-python,-c-leetcode", "fetch_time": 1726296553000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-38/segments/1725700651559.58/warc/CC-MAIN-20240914061427-20240914091427-00815.warc.gz", "warc_record_offset": 188931028, "warc_record_length": 53718, "token_count": 793, "char_count": 2478, "score": 3.984375, "int_score": 4, "crawl": "CC-MAIN-2024-38", "snapshot_type": "latest", "language": "en", "language_score": 0.8119743466377258, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00043-of-00064.parquet"}