Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7410837

Browse files
feat: add solutions to lcci problem: No.16.16 (#1654)
No.16.16.Sub Sort
1 parent ec6cc34 commit 7410837

File tree

7 files changed

+350
-3
lines changed

7 files changed

+350
-3
lines changed

‎lcci/16.16.Sub Sort/README.md‎

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## 题目描述
66

77
<!-- 这里写题目描述 -->
8+
89
<p>给定一个整数数组,编写一个函数,找出索引<code>m</code>和<code>n</code>,只要将索引区间<code>[m,n]</code>的元素排好序,整个数组就是有序的。注意:<code>n-m</code>尽量最小,也就是说,找出符合条件的最短序列。函数返回值为<code>[m,n]</code>,若不存在这样的<code>m</code>和<code>n</code>(例如整个数组是有序的),请返回<code>[-1,-1]</code>。</p>
910
<p><strong>示例:</strong></p>
1011
<pre><strong>输入:</strong> [1,2,4,7,10,11,7,12,6,7,16,18,19]
@@ -18,22 +19,147 @@
1819
## 解法
1920

2021
<!-- 这里可写通用的实现逻辑 -->
22+
23+
**方法一:两次遍历**
24+
25+
我们先从左到右遍历数组 $array,ドル用 $mx$ 记录遍历过的最大值,如果当前值 $x$ 小于 $mx,ドル则说明 $x$ 需要被排序,我们将 $x$ 的下标 $i$ 记录为 $right$;否则更新 $mx$。
26+
27+
同理,我们再从右到左遍历数组 $array,ドル用 $mi$ 记录遍历过的最小值,如果当前值 $x$ 大于 $mi,ドル则说明 $x$ 需要被排序,我们将 $x$ 的下标 $i$ 记录为 $left$;否则更新 $mi$。
28+
29+
最后返回 $[left, right]$ 即可。
30+
31+
时间复杂度 $O(n),ドル其中 $n$ 为数组长度。空间复杂度 $O(1)$。
32+
2133
<!-- tabs:start -->
2234

2335
### **Python3**
2436

2537
<!-- 这里可写当前语言的特殊实现逻辑 -->
2638

2739
```python
28-
40+
class Solution:
41+
def subSort(self, array: List[int]) -> List[int]:
42+
n = len(array)
43+
mi, mx = inf, -inf
44+
left = right = -1
45+
for i, x in enumerate(array):
46+
if x < mx:
47+
right = i
48+
else:
49+
mx = x
50+
for i in range(n - 1, -1, -1):
51+
if array[i] > mi:
52+
left = i
53+
else:
54+
mi = array[i]
55+
return [left, right]
2956
```
3057

3158
### **Java**
3259

3360
<!-- 这里可写当前语言的特殊实现逻辑 -->
3461

3562
```java
63+
class Solution {
64+
public int[] subSort(int[] array) {
65+
int n = array.length;
66+
int mi = Integer.MAX_VALUE, mx = Integer.MIN_VALUE;
67+
int left = -1, right = -1;
68+
for (int i = 0; i < n; ++i) {
69+
if (array[i] < mx) {
70+
right = i;
71+
} else {
72+
mx = array[i];
73+
}
74+
}
75+
for (int i = n - 1; i >= 0; --i) {
76+
if (array[i] > mi) {
77+
left = i;
78+
} else {
79+
mi = array[i];
80+
}
81+
}
82+
return new int[] {left, right};
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
vector<int> subSort(vector<int>& array) {
93+
int n = array.size();
94+
int mi = INT_MAX, mx = INT_MIN;
95+
int left = -1, right = -1;
96+
for (int i = 0; i < n; ++i) {
97+
if (array[i] < mx) {
98+
right = i;
99+
} else {
100+
mx = array[i];
101+
}
102+
}
103+
for (int i = n - 1; ~i; --i) {
104+
if (array[i] > mi) {
105+
left = i;
106+
} else {
107+
mi = array[i];
108+
}
109+
}
110+
return {left, right};
111+
}
112+
};
113+
```
114+
115+
### **Go**
116+
117+
```go
118+
func subSort(array []int) []int {
119+
n := len(array)
120+
mi, mx := math.MaxInt32, math.MinInt32
121+
left, right := -1, -1
122+
for i, x := range array {
123+
if x < mx {
124+
right = i
125+
} else {
126+
mx = x
127+
}
128+
}
129+
for i := n - 1; i >= 0; i-- {
130+
if array[i] > mi {
131+
left = i
132+
} else {
133+
mi = array[i]
134+
}
135+
}
136+
return []int{left, right}
137+
}
138+
```
36139

140+
### **TypeScript**
141+
142+
```ts
143+
function subSort(array: number[]): number[] {
144+
const n = array.length;
145+
let [mi, mx] = [Infinity, -Infinity];
146+
let [left, right] = [-1, -1];
147+
for (let i = 0; i < n; ++i) {
148+
if (array[i] < mx) {
149+
right = i;
150+
} else {
151+
mx = array[i];
152+
}
153+
}
154+
for (let i = n - 1; ~i; --i) {
155+
if (array[i] > mi) {
156+
left = i;
157+
} else {
158+
mi = array[i];
159+
}
160+
}
161+
return [left, right];
162+
}
37163
```
38164

39165
### **...**

‎lcci/16.16.Sub Sort/README_EN.md‎

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,136 @@
1818
<ul>
1919
<li><code>0 &lt;= len(array) &lt;= 1000000</code></li>
2020
</ul>
21+
2122
## Solutions
23+
2224
<!-- tabs:start -->
25+
2326
### **Python3**
27+
2428
```python
29+
class Solution:
30+
def subSort(self, array: List[int]) -> List[int]:
31+
n = len(array)
32+
mi, mx = inf, -inf
33+
left = right = -1
34+
for i, x in enumerate(array):
35+
if x < mx:
36+
right = i
37+
else:
38+
mx = x
39+
for i in range(n - 1, -1, -1):
40+
if array[i] > mi:
41+
left = i
42+
else:
43+
mi = array[i]
44+
return [left, right]
45+
```
2546

26-
````
2747
### **Java**
48+
2849
```java
50+
class Solution {
51+
public int[] subSort(int[] array) {
52+
int n = array.length;
53+
int mi = Integer.MAX_VALUE, mx = Integer.MIN_VALUE;
54+
int left = -1, right = -1;
55+
for (int i = 0; i < n; ++i) {
56+
if (array[i] < mx) {
57+
right = i;
58+
} else {
59+
mx = array[i];
60+
}
61+
}
62+
for (int i = n - 1; i >= 0; --i) {
63+
if (array[i] > mi) {
64+
left = i;
65+
} else {
66+
mi = array[i];
67+
}
68+
}
69+
return new int[] {left, right};
70+
}
71+
}
72+
```
73+
74+
### **C++**
2975

30-
````
76+
```cpp
77+
class Solution {
78+
public:
79+
vector<int> subSort(vector<int>& array) {
80+
int n = array.size();
81+
int mi = INT_MAX, mx = INT_MIN;
82+
int left = -1, right = -1;
83+
for (int i = 0; i < n; ++i) {
84+
if (array[i] < mx) {
85+
right = i;
86+
} else {
87+
mx = array[i];
88+
}
89+
}
90+
for (int i = n - 1; ~i; --i) {
91+
if (array[i] > mi) {
92+
left = i;
93+
} else {
94+
mi = array[i];
95+
}
96+
}
97+
return {left, right};
98+
}
99+
};
100+
```
101+
102+
### **Go**
103+
104+
```go
105+
func subSort(array []int) []int {
106+
n := len(array)
107+
mi, mx := math.MaxInt32, math.MinInt32
108+
left, right := -1, -1
109+
for i, x := range array {
110+
if x < mx {
111+
right = i
112+
} else {
113+
mx = x
114+
}
115+
}
116+
for i := n - 1; i >= 0; i-- {
117+
if array[i] > mi {
118+
left = i
119+
} else {
120+
mi = array[i]
121+
}
122+
}
123+
return []int{left, right}
124+
}
125+
```
126+
127+
### **TypeScript**
128+
129+
```ts
130+
function subSort(array: number[]): number[] {
131+
const n = array.length;
132+
let [mi, mx] = [Infinity, -Infinity];
133+
let [left, right] = [-1, -1];
134+
for (let i = 0; i < n; ++i) {
135+
if (array[i] < mx) {
136+
right = i;
137+
} else {
138+
mx = array[i];
139+
}
140+
}
141+
for (let i = n - 1; ~i; --i) {
142+
if (array[i] > mi) {
143+
left = i;
144+
} else {
145+
mi = array[i];
146+
}
147+
}
148+
return [left, right];
149+
}
150+
```
31151

32152
### **...**
33153

‎lcci/16.16.Sub Sort/Solution.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<int> subSort(vector<int>& array) {
4+
int n = array.size();
5+
int mi = INT_MAX, mx = INT_MIN;
6+
int left = -1, right = -1;
7+
for (int i = 0; i < n; ++i) {
8+
if (array[i] < mx) {
9+
right = i;
10+
} else {
11+
mx = array[i];
12+
}
13+
}
14+
for (int i = n - 1; ~i; --i) {
15+
if (array[i] > mi) {
16+
left = i;
17+
} else {
18+
mi = array[i];
19+
}
20+
}
21+
return {left, right};
22+
}
23+
};

‎lcci/16.16.Sub Sort/Solution.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func subSort(array []int) []int {
2+
n := len(array)
3+
mi, mx := math.MaxInt32, math.MinInt32
4+
left, right := -1, -1
5+
for i, x := range array {
6+
if x < mx {
7+
right = i
8+
} else {
9+
mx = x
10+
}
11+
}
12+
for i := n - 1; i >= 0; i-- {
13+
if array[i] > mi {
14+
left = i
15+
} else {
16+
mi = array[i]
17+
}
18+
}
19+
return []int{left, right}
20+
}

‎lcci/16.16.Sub Sort/Solution.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int[] subSort(int[] array) {
3+
int n = array.length;
4+
int mi = Integer.MAX_VALUE, mx = Integer.MIN_VALUE;
5+
int left = -1, right = -1;
6+
for (int i = 0; i < n; ++i) {
7+
if (array[i] < mx) {
8+
right = i;
9+
} else {
10+
mx = array[i];
11+
}
12+
}
13+
for (int i = n - 1; i >= 0; --i) {
14+
if (array[i] > mi) {
15+
left = i;
16+
} else {
17+
mi = array[i];
18+
}
19+
}
20+
return new int[] {left, right};
21+
}
22+
}

‎lcci/16.16.Sub Sort/Solution.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def subSort(self, array: List[int]) -> List[int]:
3+
n = len(array)
4+
mi, mx = inf, -inf
5+
left = right = -1
6+
for i, x in enumerate(array):
7+
if x < mx:
8+
right = i
9+
else:
10+
mx = x
11+
for i in range(n - 1, -1, -1):
12+
if array[i] > mi:
13+
left = i
14+
else:
15+
mi = array[i]
16+
return [left, right]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /