Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Índice de retorno de una matriz cuando deja de aumentar y comienza a disminuir

tiene un problema al ejecutar dos pruebas con este código, la prueba 1 devuelve la matriz completa, la prueba 2 devuelve el índice apropiado. (el punto en la matriz en el que deja de disminuir o aumentar y comienza a la inversa) si solo hay una secuencia, debería devolver -1

los casos de prueba son Entrada: [-4, -2, 9, 10] Salida: -1 Entrada: [5, 4, 3, 2, 10, 11] Salida: 3

 for (i = 0; i < arr.length; i++) { while (arr[i] < arr[i + 1]) { i++; if (arr[i] < arr[i - 1]) { return -1 } else if (arr[i] > arr[i + 1]) { return i } } while (arr[i] > arr[i + 1]) { i++; if (arr[i] > arr[i - 1]) { return -1 } else if (arr[i] < arr[i + 1]) { return i } } } return arr; }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

En la solución a continuación, el método isHomogeneous() devuelve verdadero si el contenido de la matriz aumenta o disminuye constantemente. El método isIncreasing() devuelve verdadero si el contenido de la matriz disminuye constantemente. El método isDecreasing() devuelve verdadero si el contenido de la matriz es cada vez mayor. El método getAscendingIndex() devuelve el índice en el que el contenido de la matriz comienza a disminuir. El método getDescendingIndex() devuelve el índice en el que el contenido de la matriz comienza a disminuir. El método application() contiene código de aplicación que ejecuta otros métodos. Si el contenido de la matriz no es homogéneo (aumenta o disminuye continuamente), el primer valor de índice en el que los valores comienzan a aumentar en una matriz que comienza con valores decrecientes se obtiene mediante el método getDescendingIndex() .

 /* Returns true if the array is descending to the elements (homogeneous). */ function isIncreasing(array) { for(let i = 0 ; i < array.length ; ++i) if(array[i] < array[i + 1]) return false; return true; } /* Returns true if the array is incremental (homogeneous) to the elements. */ function isDecreasing(array) { for(let i = 0 ; i < array.length ; ++i) if(array[i] > array[i + 1]) return false; return true; } /* Returns true if the array content is ever-increasing or ever-decreasing. */ function isHomogeneous(array) { return isIncreasing(array) || isDecreasing(array); } /* return the descending index in the array starting with increasing. */ function getAscendingIndex(array) { for(let i = 0 ; i < array.length ; ++i) if(array[i] > array[i + 1]) return i; return -1; } /* return increasing index in array starting with decreasing. */ function getDescendingIndex(array) { for(let i = 0 ; i < array.length ; ++i) if(array[i] < array[i + 1]) return i; return -1; } /* demo */ function application() { let firstArray = [-4, -2, 9, 10]; let secondArray = [5, 4, 3, 2, 10, 11]; console.log(`Increasing: ${(isIncreasing(firstArray) == true) ? "true" : "false"}`); console.log(`Decreasing: ${(isDecreasing(firstArray) == true) ? "true" : "false"}`); console.log(`First Array Index: ${getAscendingIndex(firstArray)}`); if(!isHomogeneous(secondArray) && getAscendingIndex(secondArray) != -1) { console.log(`Second Array Index: ${getDescendingIndex(secondArray)}`); } } application();

over 4 years ago · Santiago Trujillo Report

0

Tal vez este enfoque sería útil:

Encuentre el índice del elemento máximo y mínimo. Compruebe si este índice es el principio o el final de la matriz y devuelva -1 si lo es. Y por último, devuelva el índice máximo de los encontrados.

 const first = [-4, -2, 9, 10]; const second = [5, 4, 3, 2, 10, 11]; const getMyIndex = (arr) => { const maxIndex = arr.indexOf(Math.max(...arr)); const minIndex = arr.indexOf(Math.min(...arr)); const getResult = (index) => (index === 0) || (index === arr.length - 1) ? -1 : index; return Math.max(getResult(maxIndex), getResult(minIndex)); }; console.log('arr:', first, 'result:', getMyIndex(first)); console.log('arr:', second, 'result:', getMyIndex(second));
 .as-console-wrapper{min-height: 100%!important; top: 0}

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!