返回介绍

lcci / 16.16.Sub Sort / README_EN

发布于 2024-06-17 01:04:42 字数 4066 浏览 0 评论 0 收藏 0

16.16. Sub Sort

中文文档

Description

Given an array of integers, write a method to find indices m and n such that if you sorted elements m through n, the entire array would be sorted. Minimize n - m (that is, find the smallest such sequence).

Return [m,n]. If there are no such m and n (e.g. the array is already sorted), return [-1, -1].

Example:


Input:  [1,2,4,7,10,11,7,12,6,7,16,18,19]

Output:  [3,9]

Note:

  • 0 <= len(array) <= 1000000

Solutions

Solution 1: Two Passes

We first traverse the array $array$ from left to right, and use $mx$ to record the maximum value encountered so far. If the current value $x$ is less than $mx$, it means that $x$ needs to be sorted, and we record the index $i$ of $x$ as $right$; otherwise, update $mx$.

Similarly, we traverse the array $array$ from right to left, and use $mi$ to record the minimum value encountered so far. If the current value $x$ is greater than $mi$, it means that $x$ needs to be sorted, and we record the index $i$ of $x$ as $left$; otherwise, update $mi$.

Finally, return $[left, right]$.

The time complexity is $O(n)$, where $n$ is the length of the array $array$. The space complexity is $O(1)$.

class Solution:
  def subSort(self, array: List[int]) -> List[int]:
    n = len(array)
    mi, mx = inf, -inf
    left = right = -1
    for i, x in enumerate(array):
      if x < mx:
        right = i
      else:
        mx = x
    for i in range(n - 1, -1, -1):
      if array[i] > mi:
        left = i
      else:
        mi = array[i]
    return [left, right]
class Solution {
  public int[] subSort(int[] array) {
    int n = array.length;
    int mi = Integer.MAX_VALUE, mx = Integer.MIN_VALUE;
    int left = -1, right = -1;
    for (int i = 0; i < n; ++i) {
      if (array[i] < mx) {
        right = i;
      } else {
        mx = array[i];
      }
    }
    for (int i = n - 1; i >= 0; --i) {
      if (array[i] > mi) {
        left = i;
      } else {
        mi = array[i];
      }
    }
    return new int[] {left, right};
  }
}
class Solution {
public:
  vector<int> subSort(vector<int>& array) {
    int n = array.size();
    int mi = INT_MAX, mx = INT_MIN;
    int left = -1, right = -1;
    for (int i = 0; i < n; ++i) {
      if (array[i] < mx) {
        right = i;
      } else {
        mx = array[i];
      }
    }
    for (int i = n - 1; ~i; --i) {
      if (array[i] > mi) {
        left = i;
      } else {
        mi = array[i];
      }
    }
    return {left, right};
  }
};
func subSort(array []int) []int {
  n := len(array)
  mi, mx := math.MaxInt32, math.MinInt32
  left, right := -1, -1
  for i, x := range array {
    if x < mx {
      right = i
    } else {
      mx = x
    }
  }
  for i := n - 1; i >= 0; i-- {
    if array[i] > mi {
      left = i
    } else {
      mi = array[i]
    }
  }
  return []int{left, right}
}
function subSort(array: number[]): number[] {
  const n = array.length;
  let [mi, mx] = [Infinity, -Infinity];
  let [left, right] = [-1, -1];
  for (let i = 0; i < n; ++i) {
    if (array[i] < mx) {
      right = i;
    } else {
      mx = array[i];
    }
  }
  for (let i = n - 1; ~i; --i) {
    if (array[i] > mi) {
      left = i;
    } else {
      mi = array[i];
    }
  }
  return [left, right];
}

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

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

发布评论

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