• 作者:老汪软件技巧
  • 发表时间:2024-10-30 10:03
  • 浏览量:

问题描述

小S正在玩一个关于石子的游戏,游戏中给定了一些石子,它们位于一维数轴上的不同位置,位置用数组stones表示。如果某个石子处于最小或最大的一个位置,我们称其为端点石子。

在每个回合,小S可以将一颗端点石子移动到一个未占用的位置,使其不再是端点石子。值得注意的是,如果石子的位置是连续的,则游戏结束,因为没有可以进行的移动操作。

你需要帮助小S找到可以移动的最小次数。

测试样例

样例1:

输入:stones = [7, 4, 9]

输出:1

样例2:

输入:stones = [6, 5, 4, 3, 10]

输出:2

ipad上怎么玩网页游戏_石子游戏算法_

样例3:

输入:stones = [1, 2, 3, 4, 5]

输出:0

Java代码

 import java.util.Arrays;
  public class Main {
  public static int solution(int[] stones) {
    if (stones.length == 1) {
        return 0;
    }
    
    Arrays.sort(stones);
    int n = stones.length;
    // 特殊处理 n = 2 的情况
    if (n == 2) {
        if (stones[1] - stones[0] == 1) {
            return 0; // 相邻则不需要移动
        }
        return 2; // 不相邻则需要移动两次
    }
    // 计算最大移动次数
    int maxMoves = stones[n - 1] - stones[0] + 1 - n;
    // 计算最小移动次数,使用滑动窗口
    int minMoves = Integer.MAX_VALUE;
    int j = 0;
    for (int i = 0; i < n; i++) {
        // 确保窗口内最多有 n 个石子
        while (j < n && stones[j] - stones[i] + 1 <= n) {
            j++;
        }
        // 如果窗口内有 n - 1 个石子,且空位为 1,则需要特殊处理
        int alreadyInPlace = j - i;
        if (alreadyInPlace == n - 1 && stones[j - 1] - stones[i] + 1 == n - 1) {
            minMoves = Math.min(minMoves, 2);
        } else {
            minMoves = Math.min(minMoves, n - alreadyInPlace);
        }
    }
    return Math.min(minMoves, maxMoves);
}
public static void main(String[] args) {
    System.out.println(solution(new int[]{7, 4, 9}) == 1); // 输出:true
    System.out.println(solution(new int[]{6, 5, 4, 3, 10}) == 2); // 输出:true
    System.out.println(solution(new int[]{1, 2, 3, 4, 5}) == 0); // 输出:true
    System.out.println(solution(new int[]{6, 8}) == 1); // 输出:true
    System.out.println(solution(new int[]{11, 14}) == 2); // 输出:true
}
}

时间复杂度分析

排序操作

特殊处理 n = 2 的情况:

计算最大移动次数:

计算最小移动次数,使用滑动窗口:

总时间复杂度

综合起来,代码的总时间复杂度为O(n log n),其中n是数组stones的长度。