Algorithm


Problem Name: 1033. Moving Stones Until Consecutive

There are three stones in different positions on the X-axis. You are given three integers a, b, and c, the positions of the stones.

In one move, you pick up a stone at an endpoint (i.e., either the lowest or highest position stone), and move it to an unoccupied position between those endpoints. Formally, let's say the stones are currently at positions x, y, and z with x < y < z. You pick up the stone at either position x or position z, and move that stone to an integer position k, with x < k < z and k != y.

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: a = 1, b = 2, c = 5
Output: [1,2]
Explanation: Move the stone from 5 to 3, or move the stone from 5 to 4 to 3.

Example 2:

Input: a = 4, b = 3, c = 2
Output: [0,0]
Explanation: We cannot make any moves.

Example 3:

Input: a = 3, b = 5, c = 1
Output: [1,2]
Explanation: Move the stone from 1 to 4; or move the stone from 1 to 2 to 4.

 

Constraints:

  • 1 <= a, b, c <= 100
  • a, b, and c have different values.

Code Examples

#1 Code Example with Javascript Programming

Code - Javascript Programming


const numMovesStones = function(a, b, c) {
    let min = 0
    let min2 = 0
    let max = 0
    const arr= [a,b,c]
    arr.sort((a,b) => a - b)
    
    max = arr[2]-arr[1]-1 + arr[1] - arr[0] - 1
    min = (arr[2] - arr[1] > 1 ? 1 : 0) +(arr[1] - arr[0] > 1 ? 1 : 0)
    min2 = arr[2] - arr[1] === 2 || arr[1] - arr[0] === 2 ? 1 : Number.MAX_VALUE
  
    return [Math.min(min, min2), max]
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
a = 1, b = 2, c = 5

Output

x
+
cmd
[1,2]

#2 Code Example with Python Programming

Code - Python Programming


class Solution:
    def numMovesStones(self, a: int, b: int, c: int) -> List[int]:
        a, b, c = sorted([a, b, c])
        m1 = b - 1 - a
        m2 = c - b - 1
        m1 + m2
        if a + 2 == b or b + 2 == c:
            return [1, m1 + m2]
        n1 = int(b - 1 > a)
        n2 = int(b + 1 < c)
        return [n1 + n2, m1 + m2]       
                
Copy The Code & Try With Live Editor

Input

x
+
cmd
a = 1, b = 2, c = 5

Output

x
+
cmd
[1,2]

#3 Code Example with C# Programming

Code - C# Programming


using System;

namespace LeetCode
{
    public class _1033_MovingStonesUntilConsecutive
    {
        public int[] NumMovesStones(int a, int b, int c)
        {
            var arr = new int[] { a, b, c };
            Array.Sort(arr);

            var leftGap = arr[1] - arr[0] - 1;
            var rightGap = arr[2] - arr[1] - 1;

            if (leftGap == 0 && rightGap == 0) return new int[] { 0, 0 };
            var min = 2;
            if (leftGap == 0 || rightGap == 0) min = 1;
            if (leftGap == 1 || rightGap == 1) min = 1;

            return new int[] { min, leftGap + rightGap };
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
a = 4, b = 3, c = 2

Output

x
+
cmd
[0,0
Advertisements

Demonstration


Previous
#1032 Leetcode Stream of Characters Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1034 Leetcode Coloring A Border Solution in C, C++, Java, JavaScript, Python, C# Leetcode