1040. Moving Stones Until Consecutive II Medium

@problem@discussion
#Array#Math#Two Pointers#Sorting



1/**
2 * [1040] Moving Stones Until Consecutive II
3 *
4 * There are some stones in different positions on the X-axis. You are given an integer array stones, the positions of the stones.
5 * 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.
6 * 
7 * 	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.
8 * 
9 * The game ends when you cannot make any more moves (i.e., the stones are in three consecutive positions).
10 * Return an integer array answer of length 2 where:
11 * 
12 * 	answer[0] is the minimum number of moves you can play, and
13 * 	answer[1] is the maximum number of moves you can play.
14 * 
15 *  
16 * Example 1:
17 * 
18 * Input: stones = [7,4,9]
19 * Output: [1,2]
20 * Explanation: We can move 4 -> 8 for one move to finish the game.
21 * Or, we can move 9 -> 5, 4 -> 6 for two moves to finish the game.
22 * 
23 * Example 2:
24 * 
25 * Input: stones = [6,5,4,3,10]
26 * Output: [2,3]
27 * Explanation: We can move 3 -> 8 then 10 -> 7 to finish the game.
28 * Or, we can move 3 -> 7, 4 -> 8, 5 -> 9 to finish the game.
29 * Notice we cannot move 10 -> 2 to finish the game, because that would be an illegal move.
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	3 <= stones.length <= 10^4
35 * 	1 <= stones[i] <= 10^9
36 * 	All the values of stones are unique.
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/moving-stones-until-consecutive-ii/
42// discuss: https://leetcode.com/problems/moving-stones-until-consecutive-ii/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn num_moves_stones_ii(stones: Vec<i32>) -> Vec<i32> {
48        vec![]
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_1040() {
60    }
61}
62


Back
© 2025 bowen.ge All Rights Reserved.