Movatterモバイル変換


[0]ホーム

URL:


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

Commit5cc8741

Browse files
frankymacster64json
authored andcommitted
Add Shortest Unsorted Continous Subarray (#20)
1 parent0a096f4 commit5cc8741

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#Shortest Unsorted Continous Subarray
2+
"Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.
3+
4+
You need to find the shortest such subarray and output its length."
5+
6+
##Complexity
7+
***Time**: worst![](https://latex.codecogs.com/svg.latex?O(N)), 4 loops are used
8+
***Space**: worst![](https://latex.codecogs.com/svg.latex?O(1)), to hold min and max values
9+
10+
##References
11+
*[LeetCode](https://leetcode.com/articles/shortest-unsorted-continous-subarray/)
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// import visualization libraries {
2+
const{ Tracer, LogTracer, Array1DTracer, Layout, VerticalLayout}=require('algorithm-visualizer');
3+
// }
4+
5+
// define tracer variables {
6+
consttracer=newArray1DTracer('Sequence');
7+
constD=[2,6,4,8,10,9,15];
8+
tracer.set(D);
9+
constlogger=newLogTracer();
10+
Layout.setRoot(newVerticalLayout([tracer,logger]));
11+
Tracer.delay();
12+
// }
13+
14+
functionfindUnsortedSubarray(nums){
15+
letmin=Number.MAX_VALUE;
16+
letmax=Number.MIN_VALUE;
17+
letflag=false;
18+
// visualize {
19+
letminIndex=-1;
20+
letmaxIndex=-1;
21+
// }
22+
23+
for(leti=1;i<nums.length;i++){
24+
// visualize {
25+
tracer.deselect(i-2,i-1);
26+
tracer.select(i-1,i);
27+
Tracer.delay();
28+
// }
29+
30+
if(nums[i]<nums[i-1]){
31+
flag=true;
32+
}
33+
if(flag){
34+
min=Math.min(min,nums[i]);
35+
// visualize {
36+
if(min===nums[i]){
37+
tracer.depatch(minIndex);
38+
minIndex=i;
39+
tracer.patch(i);
40+
}
41+
Tracer.delay();
42+
// }
43+
}
44+
}
45+
46+
// visualize {
47+
tracer.depatch(minIndex);
48+
tracer.deselect(nums.length-2);
49+
tracer.deselect(nums.length-1);
50+
// }
51+
52+
// logger {
53+
logger.println(`min =${min}`);
54+
Tracer.delay();
55+
// }
56+
57+
flag=false;
58+
for(leti=nums.length-2;i>=0;i--){
59+
// visualize {
60+
tracer.deselect(i+1,i+2);
61+
tracer.select(i,i+1);
62+
Tracer.delay();
63+
// }
64+
65+
if(nums[i]>nums[i+1]){
66+
flag=true;
67+
}
68+
if(flag){
69+
max=Math.max(max,nums[i]);
70+
// visualize {
71+
if(max===nums[i]){
72+
tracer.depatch(maxIndex);
73+
maxIndex=i;
74+
tracer.patch(i);
75+
}
76+
Tracer.delay();
77+
// }
78+
}
79+
}
80+
81+
// visualize {
82+
tracer.depatch(maxIndex);
83+
tracer.deselect(0);
84+
tracer.deselect(1);
85+
Tracer.delay();
86+
// }
87+
88+
// logger {
89+
logger.println(`max =${max}`);
90+
// }
91+
92+
letl;
93+
letr;
94+
for(l=0;l<nums.length;l++){
95+
// visualize {
96+
tracer.deselect(l-1);
97+
tracer.select(l);
98+
Tracer.delay();
99+
// }
100+
101+
if(min<nums[l]){
102+
// visualize {
103+
tracer.patch(l);
104+
Tracer.delay();
105+
// }
106+
break;
107+
}
108+
}
109+
110+
for(r=nums.length-1;r>=0;r--){
111+
// visualize {
112+
tracer.deselect(r+1);
113+
tracer.select(r);
114+
Tracer.delay();
115+
// }
116+
117+
if(max>nums[r]){
118+
// visualize {
119+
tracer.patch(r);
120+
Tracer.delay();
121+
// }
122+
break;
123+
}
124+
}
125+
126+
// visualize {
127+
tracer.depatch(l);
128+
tracer.depatch(r);
129+
tracer.select(l,r);
130+
Tracer.delay();
131+
// }
132+
133+
constresult=r-l<0
134+
?0
135+
:r-l+1;
136+
137+
// logger {
138+
logger.println(`result =${result}`);
139+
Tracer.delay();
140+
// }
141+
142+
returnresult;
143+
}
144+
findUnsortedSubarray(D);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp