返回介绍

solution / 1000-1099 / 1040.Moving Stones Until Consecutive II / README_EN

发布于 2024-06-17 01:03:31 字数 5296 浏览 0 评论 0 收藏 0

1040. Moving Stones Until Consecutive II

中文文档

Description

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.

Solutions

Solution 1

class Solution:
  def numMovesStonesII(self, stones: List[int]) -> List[int]:
    stones.sort()
    mi = n = len(stones)
    mx = max(stones[-1] - stones[1] + 1, stones[-2] - stones[0] + 1) - (n - 1)
    i = 0
    for j, x in enumerate(stones):
      while x - stones[i] + 1 > n:
        i += 1
      if j - i + 1 == n - 1 and x - stones[i] == n - 2:
        mi = min(mi, 2)
      else:
        mi = min(mi, n - (j - i + 1))
    return [mi, mx]
class Solution {
  public int[] numMovesStonesII(int[] stones) {
    Arrays.sort(stones);
    int n = stones.length;
    int mi = n;
    int mx = Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1);
    for (int i = 0, j = 0; j < n; ++j) {
      while (stones[j] - stones[i] + 1 > n) {
        ++i;
      }
      if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) {
        mi = Math.min(mi, 2);
      } else {
        mi = Math.min(mi, n - (j - i + 1));
      }
    }
    return new int[] {mi, mx};
  }
}
class Solution {
public:
  vector<int> numMovesStonesII(vector<int>& stones) {
    sort(stones.begin(), stones.end());
    int n = stones.size();
    int mi = n;
    int mx = max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1);
    for (int i = 0, j = 0; j < n; ++j) {
      while (stones[j] - stones[i] + 1 > n) {
        ++i;
      }
      if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) {
        mi = min(mi, 2);
      } else {
        mi = min(mi, n - (j - i + 1));
      }
    }
    return {mi, mx};
  }
};
func numMovesStonesII(stones []int) []int {
  sort.Ints(stones)
  n := len(stones)
  mi := n
  mx := max(stones[n-1]-stones[1]+1, stones[n-2]-stones[0]+1) - (n - 1)
  i := 0
  for j, x := range stones {
    for x-stones[i]+1 > n {
      i++
    }
    if j-i+1 == n-1 && stones[j]-stones[i] == n-2 {
      mi = min(mi, 2)
    } else {
      mi = min(mi, n-(j-i+1))
    }
  }
  return []int{mi, mx}
}
function numMovesStonesII(stones: number[]): number[] {
  stones.sort((a, b) => a - b);
  const n = stones.length;
  let mi = n;
  const mx = Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1);
  for (let i = 0, j = 0; j < n; ++j) {
    while (stones[j] - stones[i] + 1 > n) {
      ++i;
    }
    if (j - i + 1 === n - 1 && stones[j] - stones[i] === n - 2) {
      mi = Math.min(mi, 2);
    } else {
      mi = Math.min(mi, n - (j - i + 1));
    }
  }
  return [mi, mx];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文