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:
checkSledJump([1, 2, 3, 2, 1]) // true: strictly increasing and then strictly decreasing checkSledJump([0, 1, 0]) // -> true: strictly increasing and then strictly decreasing checkSledJump([0, 3, 2, 1]) // -> true: strictly increasing and then strictly decreasing checkSledJump([0, 1000, 1]) // -> true: strictly increasing and then strictly decreasing checkSledJump([2, 4, 4, 6, 2]) // false: [4,4] isn't strictly increasing or decreasing checkSledJump([1, 2, 3]) // false: only increasing checkSledJump([3, 2, 1]) // false: only decreasing checkSledJump([1, 2, 3, 2, 1, 2, 3]) // false: increasing then decreasing then increasing
My solution:
function checkSledJump(heights) { let max = Math.max(...heights); let maxIndex = heights.indexOf(max); if (maxIndex === 0 || maxIndex === heights.length-1) return false let strictlyIncreasing = heights.slice(0, maxIndex+1) let strictlyDecreasing = heights.slice(maxIndex); for(let i = 0; i < strictlyIncreasing.length - 1; i++) if(!(strictlyIncreasing[i] < strictlyIncreasing[i+1])) return false for(let i = 0; i < strictlyDecreasing.length - 1; i++) if(!(strictlyDecreasing[i] > strictlyDecreasing[i+1])) return false return true }
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.
function checkSledJump(heights) { let graph = []; for (let i = 0; i < heights.length - 1; i++) { const diff = heights[i + 1] - heights[i]; if(!diff) return false; graph.push(diff > 0 ? 1 : 0); } const graphString = graph.join(''); return graphString.lastIndexOf('1') + 1 && graphString.lastIndexOf('1') < graphString.indexOf('0') ? true : false; // pattern increment => decrement //return graphString.lastIndexOf('0') + 1 && graphString.lastIndexOf('0') < graphString.indexOf('1')? true : false; // pattern decrement => increment } console.log(checkSledJump([1, 2, 3, 2, 1])); // true: strictly increasing and then strictly decreasing console.log(checkSledJump([0, 1, 0])); // -> true: strictly increasing and then strictly decreasing console.log(checkSledJump([0, 3, 2, 1])); // -> true: strictly increasing and then strictly decreasing console.log(checkSledJump([0, 1000, 1])); // -> true: strictly increasing and then strictly decreasing console.log(checkSledJump([2, 4, 4, 6, 2])); // false: [4,4] isn't strictly increasing or decreasing console.log(checkSledJump([1, 2, 3])); // false: only increasing console.log(checkSledJump([3, 2, 1])); // false: only decreasing console.log(checkSledJump([1, 2, 3, 2, 1, 2, 3])); // false: increasing then decreasing then increasing console.log(checkSledJump([1, 2, 3, 2, 2, 1]));