comments | difficulty | edit_url |
---|---|---|
true |
中等 |
给定一个整数数组,编写一个函数,找出索引m
和n
,只要将索引区间[m,n]
的元素排好序,整个数组就是有序的。注意:n-m
尽量最小,也就是说,找出符合条件的最短序列。函数返回值为[m,n]
,若不存在这样的m
和n
(例如整个数组是有序的),请返回[-1,-1]
。
示例:
输入: [1,2,4,7,10,11,7,12,6,7,16,18,19] 输出: [3,9]
提示:
0 <= len(array) <= 1000000
我们先从左到右遍历数组
同理,我们再从右到左遍历数组
最后返回
时间复杂度
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];
}
class Solution {
func subSort(_ array: [Int]) -> [Int] {
let n = array.count
var mi = Int.max, mx = Int.min
var left = -1, right = -1
for i in 0..<n {
if array[i] < mx {
right = i
} else {
mx = array[i]
}
}
for i in stride(from: n - 1, through: 0, by: -1) {
if array[i] > mi {
left = i
} else {
mi = array[i]
}
}
return [left, right]
}
}