Resolví un ejercicio en javascript pero no estoy muy contento con mi solución y no pude encontrar una mejor.
Problema: compruebe si una matriz tiene un subarreglo inicial estrictamente creciente y un subarreglo final estrictamente decreciente. Cada matriz tiene al menos 3 elementos.
Ejemplos:
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 increasingMi solución:
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 }Hay una mejor manera de hacerlo? ¿Quizás usando reducir?
Gracias.
Puede crear un gráfico de diferencia y verificar el patrón específico.
Aquí puede encontrar dos patrones, la primera condición de retorno verifica si los elementos de la matriz siguen un patrón de incremento y luego decremento.
La segunda condición de retorno verifica si los elementos de la matriz siguen y disminuyen y luego incrementan el patrón.
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]));