I solved an exercise in javascript but I’m not very happy with my solution and I couldn’t find a better one.
Problem: check if an array have a starting subarray strictly increasing and an ending subarray strictly decreasing. Each array has at least 3 elements.
Examples:
JavaScript
x
10
10
1
checkSledJump([1, 2, 3, 2, 1]) // true: strictly increasing and then strictly decreasing
2
checkSledJump([0, 1, 0]) // -> true: strictly increasing and then strictly decreasing
3
checkSledJump([0, 3, 2, 1]) // -> true: strictly increasing and then strictly decreasing
4
checkSledJump([0, 1000, 1]) // -> true: strictly increasing and then strictly decreasing
5
6
checkSledJump([2, 4, 4, 6, 2]) // false: [4,4] isn't strictly increasing or decreasing
7
checkSledJump([1, 2, 3]) // false: only increasing
8
checkSledJump([3, 2, 1]) // false: only decreasing
9
checkSledJump([1, 2, 3, 2, 1, 2, 3]) // false: increasing then decreasing then increasing
10
My solution:
JavaScript
1
17
17
1
function checkSledJump(heights) {
2
let max = Math.max(heights);
3
let maxIndex = heights.indexOf(max);
4
if (maxIndex === 0 || maxIndex === heights.length-1) return false
5
6
let strictlyIncreasing = heights.slice(0, maxIndex+1)
7
let strictlyDecreasing = heights.slice(maxIndex);
8
9
for(let i = 0; i < strictlyIncreasing.length - 1; i++)
10
if(!(strictlyIncreasing[i] < strictlyIncreasing[i+1])) return false
11
12
for(let i = 0; i < strictlyDecreasing.length - 1; i++)
13
if(!(strictlyDecreasing[i] > strictlyDecreasing[i+1])) return false
14
15
return true
16
}
17
Is there a better way to do it? Maybe using reduce?
Thanks.
Advertisement
Answer
You can create difference graph and check for the specific pattern.
Here you can find two patterns first return condition check whether array elements follows increment and then decrement pattern.
Second return condition check whether array elements follows and decrement then increment pattern.
JavaScript
1
23
23
1
function checkSledJump(heights) {
2
let graph = [];
3
for (let i = 0; i < heights.length - 1; i++) {
4
const diff = heights[i + 1] - heights[i];
5
if(!diff) return false;
6
graph.push(diff > 0 ? 1 : 0);
7
}
8
9
const graphString = graph.join('');
10
11
return graphString.lastIndexOf('1') + 1 && graphString.lastIndexOf('1') < graphString.indexOf('0') ? true : false; // pattern increment => decrement
12
//return graphString.lastIndexOf('0') + 1 && graphString.lastIndexOf('0') < graphString.indexOf('1')? true : false; // pattern decrement => increment
13
}
14
15
console.log(checkSledJump([1, 2, 3, 2, 1])); // true: strictly increasing and then strictly decreasing
16
console.log(checkSledJump([0, 1, 0])); // -> true: strictly increasing and then strictly decreasing
17
console.log(checkSledJump([0, 3, 2, 1])); // -> true: strictly increasing and then strictly decreasing
18
console.log(checkSledJump([0, 1000, 1])); // -> true: strictly increasing and then strictly decreasing
19
console.log(checkSledJump([2, 4, 4, 6, 2])); // false: [4,4] isn't strictly increasing or decreasing
20
console.log(checkSledJump([1, 2, 3])); // false: only increasing
21
console.log(checkSledJump([3, 2, 1])); // false: only decreasing
22
console.log(checkSledJump([1, 2, 3, 2, 1, 2, 3])); // false: increasing then decreasing then increasing
23
console.log(checkSledJump([1, 2, 3, 2, 2, 1]));