返回介绍

solution / 2300-2399 / 2340.Minimum Adjacent Swaps to Make a Valid Array / README_EN

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

2340. Minimum Adjacent Swaps to Make a Valid Array

中文文档

Description

You are given a 0-indexed integer array nums.

Swaps of adjacent elements are able to be performed on nums.

A valid array meets the following conditions:

  • The largest element (any of the largest elements if there are multiple) is at the rightmost position in the array.
  • The smallest element (any of the smallest elements if there are multiple) is at the leftmost position in the array.

Return _the minimum swaps required to make _nums_ a valid array_.

 

Example 1:

Input: nums = [3,4,5,5,3,1]
Output: 6
Explanation: Perform the following swaps:
- Swap 1: Swap the 3rd and 4th elements, nums is then [3,4,5,3,5,1].
- Swap 2: Swap the 4th and 5th elements, nums is then [3,4,5,3,1,5].
- Swap 3: Swap the 3rd and 4th elements, nums is then [3,4,5,1,3,5].
- Swap 4: Swap the 2nd and 3rd elements, nums is then [3,4,1,5,3,5].
- Swap 5: Swap the 1st and 2nd elements, nums is then [3,1,4,5,3,5].
- Swap 6: Swap the 0th and 1st elements, nums is then [1,3,4,5,3,5].
It can be shown that 6 swaps is the minimum swaps required to make a valid array.

Example 2:

Input: nums = [9]
Output: 0
Explanation: The array is already valid, so we return 0.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 105

Solutions

Solution 1

class Solution:
  def minimumSwaps(self, nums: List[int]) -> int:
    i = j = 0
    for k, v in enumerate(nums):
      if v < nums[i] or (v == nums[i] and k < i):
        i = k
      if v >= nums[j] or (v == nums[j] and k > j):
        j = k
    return 0 if i == j else i + len(nums) - 1 - j - (i > j)
class Solution {
  public int minimumSwaps(int[] nums) {
    int n = nums.length;
    int i = 0, j = 0;
    for (int k = 0; k < n; ++k) {
      if (nums[k] < nums[i] || (nums[k] == nums[i] && k < i)) {
        i = k;
      }
      if (nums[k] > nums[j] || (nums[k] == nums[j] && k > j)) {
        j = k;
      }
    }
    if (i == j) {
      return 0;
    }
    return i + n - 1 - j - (i > j ? 1 : 0);
  }
}
class Solution {
public:
  int minimumSwaps(vector<int>& nums) {
    int n = nums.size();
    int i = 0, j = 0;
    for (int k = 0; k < n; ++k) {
      if (nums[k] < nums[i] || (nums[k] == nums[i] && k < i)) {
        i = k;
      }
      if (nums[k] > nums[j] || (nums[k] == nums[j] && k > j)) {
        j = k;
      }
    }
    if (i == j) {
      return 0;
    }
    return i + n - 1 - j - (i > j);
  }
};
func minimumSwaps(nums []int) int {
  var i, j int
  for k, v := range nums {
    if v < nums[i] || (v == nums[i] && k < i) {
      i = k
    }
    if v > nums[j] || (v == nums[j] && k > j) {
      j = k
    }
  }
  if i == j {
    return 0
  }
  if i < j {
    return i + len(nums) - 1 - j
  }
  return i + len(nums) - 2 - j
}
function minimumSwaps(nums: number[]): number {
  let i = 0;
  let j = 0;
  const n = nums.length;
  for (let k = 0; k < n; ++k) {
    if (nums[k] < nums[i] || (nums[k] == nums[i] && k < i)) {
      i = k;
    }
    if (nums[k] > nums[j] || (nums[k] == nums[j] && k > j)) {
      j = k;
    }
  }
  return i == j ? 0 : i + n - 1 - j - (i > j ? 1 : 0);
}

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

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

发布评论

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