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

124
Views
cómo obtener el índice ordenado de valores mínimos de matriz 2d

tener una matriz de números 2d como;

 const arr = [ [1, 5, 9], [2, 7, 8], [3, 0, 6], ];

¿Cuál es la forma más sencilla de obtener una matriz ordenada de índices de matriz donde los criterios de clasificación son valores de la matriz 2d original?

el resultado debe ser:

 `[2,1]`, // (value=0) `[0,0]`, // (value=1) `[2,0]`, // (value=2) `[0,1]`, // (value=3) ...

por cierto, los valores reales son flotantes, no es que importe.
pero la complejidad importa ya que el bucle se ejecuta en cada cuadro.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Podría obtener los índices primero y ordenarlos por el valor de la matriz.

 const array = [[1, 5, 9], [2, 7, 8], [3, 0, 6]], result = array .flatMap((a, i) => a.map((_, j) => [i, j])) .sort((a, b) => array[a[0]][a[1]] - array[b[0]][b[1]]); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

¿Algo como esto?

 const arr = [ [1, 5, 9], [2, 7, 8], [3, 0, 6], ]; const map = [] arr.forEach((row, rowIndex) => row.forEach((col, colIndex) => { map.push({ c: colIndex, r: rowIndex, value: col }); })) console.log(map) const sorted = map.sort((a, b) => { if(a.value === b.value) return 0; return a.value > b.value ? 1 : -1; }) console.log(sorted) console.log(sorted.map(v => [vr, vc]))

es una versión de trabajo, no optimizada

EDITAR: poca optimización con medida de tiempo de ejecución:

 const arr = [ [1, 5, 9], [2, 7, 8], [3, 0, 6], ]; const sortFn = (a, b) => a.value === b.value ? 0 : a.value > b.value ? 1 : -1; let count = 3; function process(arr) { const map = [] arr.forEach((row, rowIndex) => row.forEach((col, colIndex) => { map.push({ row: rowIndex, col: colIndex, value: col }); })) return map.sort(sortFn).map(v => [v.row, v.col]); } const interval = setInterval(() => { console.time("process"); console.log(process(arr)) console.timeEnd("process"); if(--count === 0) clearInterval(interval); }, 100)

Producción:

 [ [ 2, 1 ], [ 0, 0 ], [ 1, 0 ], [ 2, 0 ], [ 0, 1 ], [ 2, 2 ], [ 1, 1 ], [ 1, 2 ], [ 0, 2 ] ] process: 3.822ms [ [ 2, 1 ], [ 0, 0 ], [ 1, 0 ], [ 2, 0 ], [ 0, 1 ], [ 2, 2 ], [ 1, 1 ], [ 1, 2 ], [ 0, 2 ] ] process: 1.661ms [ [ 2, 1 ], [ 0, 0 ], [ 1, 0 ], [ 2, 0 ], [ 0, 1 ], [ 2, 2 ], [ 1, 1 ], [ 1, 2 ], [ 0, 2 ] ] process: 1.665ms

EDIT 2: (inspirado en otra respuesta con flatMap )

 const arr = [ [1, 5, 9], [2, 7, 8], [3, 0, 6], ]; const sortFn = (a, b) => a.value === b.value ? 0 : a.value > b.value ? 1 : -1; arr.flatMap((_row, row) => _row.map((value, col) => { return { row, col, value } })).sort(sortFn).map(v => [v.row, v.col]); let count = 3; const process = (arr) => arr.flatMap((_row, row) => _row.map((value, col) => { return { row, col, value } })).sort(sortFn).map(v => [v.row, v.col]); const interval = setInterval(() => { console.time("process"); console.log(process(arr)) console.timeEnd("process"); if(--count === 0) clearInterval(interval); }, 100)

Resultado:

 [ [ 2, 1 ], [ 0, 0 ], [ 1, 0 ], [ 2, 0 ], [ 0, 1 ], [ 2, 2 ], [ 1, 1 ], [ 1, 2 ], [ 0, 2 ] ] process: 10.979ms [ [ 2, 1 ], [ 0, 0 ], [ 1, 0 ], [ 2, 0 ], [ 0, 1 ], [ 2, 2 ], [ 1, 1 ], [ 1, 2 ], [ 0, 2 ] ] process: 1.346ms [ [ 2, 1 ], [ 0, 0 ], [ 1, 0 ], [ 2, 0 ], [ 0, 1 ], [ 2, 2 ], [ 1, 1 ], [ 1, 2 ], [ 0, 2 ] ] process: 2.043ms
about 4 years ago · Juan Pablo Isaza 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!