Skip to content

Commit 77c25be

Browse files
committed
added problem smallest_sort_window
1 parent 1243666 commit 77c25be

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

easy/smallest_sort_window/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Given an array of integers out of order, determine the bounds of the smallest window that must be sorted in order for the entire array to be sorted. For example, given [3, 7, 5, 6, 9], you should return (1, 3).
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} arr
3+
* @returns {number[]}
4+
*/
5+
function smallestSortWindow(arr) {
6+
let s = -1;
7+
let f = -1;
8+
9+
for (let i = 0; i < arr.length; i++) {
10+
if (s !== -1) {
11+
if (arr[s] > arr[i]) {
12+
f = i;
13+
} else if (arr[i] > arr[i + 1]) {
14+
f = i + 1;
15+
}
16+
} else if (arr[i] > arr[i + 1]) {
17+
s = i;
18+
}
19+
}
20+
21+
return [s, f];
22+
}
23+
24+
module.exports = smallestSortWindow;

easy/smallest_sort_window/test.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"title": "smallest_sort_window",
3+
"tests": [
4+
{
5+
"desc": 1,
6+
"args": [[3, 7, 5, 6, 9]],
7+
"exp": [1, 3]
8+
},
9+
{
10+
"desc": 2,
11+
"args": [[1, 2, 3, 7, 5, 6, 4, 8]],
12+
"exp": [3, 6]
13+
},
14+
{
15+
"desc": 3,
16+
"args": [[2, 6, 4, 8, 10, 9, 15]],
17+
"exp": [1, 5]
18+
}
19+
]
20+
}

0 commit comments

Comments
 (0)