Быстрый способ найти индекс элемента max / min в N-м столбце матрицы - PullRequest
2 голосов
/ 20 апреля 2020

Я пытаюсь получить индекс максимального и минимального элемента указанного столбца c в матрице. Теперь я делаю следующее с использованием синтаксиса ES6 и Spread:

a = [
  [22,23],
  [74,1],
  [21,33],
  [32,84],
  [11,31],
  [1,49],
  [7,8],
  [11,11],
  [99,68],
  [52,20]
];

const minValue = (arr, n) => Math.min(...arr.map(x => x[n])); //n - column index
const maxValue = (arr, n) => Math.max(...arr.map(x => x[n]));

const minValueIndex = (arr, n) => arr.map(x => x[n]).indexOf(minValue(arr, n));
const maxValueIndex = (arr, n) => arr.map(x => x[n]).indexOf(maxValue(arr, n));

console.log(minValue(a, 0));
console.log(maxValue(a, 0));

console.log(minValueIndex(a, 0));
console.log(maxValueIndex(a, 0));

Но в то же время проводится слишком много сравнений, я не думаю, что это лучший способ выполнить эту задачу. Я был бы рад видеть более быстрые реализации, использующие стандарт ES6 или без него.

Ответы [ 3 ]

1 голос
/ 20 апреля 2020

Простым решением будет l oop над массивом и сохранение значений min / max во временной переменной.

function minMax (arr, n) {
    let min=Infinity, max=0;
    for (const _arr of arr) {
        const x = _arr[n];
        if (x < min) min = x;
        if (x > max) max = x;
    }
    return [min, max];
}

function minMaxIndex (arr, n) {
    let min=Infinity, max=0, minIndex, maxIndex;
    for (let i=0; i < arr.length; i++) {
        const x = arr[i][n];
        if (x < min) {
          min = x;
          minIndex = i;
        }
        if (x > max) {
          max = x;
          maxIndex = i;
        }
    }
    return [minIndex, maxIndex];
}

console.log (minMax(a, 0))
console.log (minMaxIndex(a, 0))
<script>
a = [
  [22,23],
  [74,1],
  [21,33],
  [32,84],
  [11,31],
  [1,49],
  [7,8],
  [11,11],
  [99,68],
  [52,20]
];
</script>
1 голос
/ 20 апреля 2020

Поможет ли это?

let a = [
    [22, 23],
    [74, 1],
    [21, 33],
    [32, 84],
    [11, 31],
    [1, 49],
    [7, 8],
    [11, 11],
    [99, 68],
    [52, 20]
];
let max = 0,
    min = 0,
    minIndex = 0,
    maxIndex = 0;

const findValue = (array, col) => {
    array.map((matrix) => {
        (matrix[col] > max) ? max = matrix[col]: null;
        (min == 0) ? min = max: null;
        (matrix[col] < min) ? min = matrix[col]: null;
    })
}
const findIndex = (array, col, min, max) => {
    minIndex = array.map(data => data[col]).indexOf(min);
    maxIndex = array.map(data => data[col]).indexOf(max);
}

findValue(a, 0)
findIndex(a, 0, min, max);
console.log(min, max, minIndex, maxIndex);
1 голос
/ 20 апреля 2020

Вы почти на месте, и вы просто беспокоитесь о производительности, верно? Таким образом, для повышения производительности вашей программы вы можете использовать приятную технику, которая называется Memoization

Запоминание - это метод оптимизации, который используется главным образом для ускорения работы компьютерных программ путем сохранения результатов. дорогостоящих вызовов функций и возврата кэшированного результата, когда те же самые входы повторяются

const arr = [[22,23], [74,1], [21,33], [32,84], [11,31], [1,49], [7,8], [11,11], [99,68], [52,20]];

/**
* Here I create the momoized function which cache the
* column and if we want to get the same column then it
* simply return the previously cached column array
* otherwise, it get the column and cache it for future
* and return it.
*/
const memoized = () => {
	const cache = {};

	return (arr, index) => {
	    if (index in cache) {
	        return cache[index];
	    } else {
		const col = arr.map(item => (item[index]));
		cache[index] = col;
		return col;
	    }
	}
}

/**
* As memoized is a higher order function so it returns
* another function which will be executed by calling
* this getColumn function reference.
*/
const getColumn = memoized();

const getMinValue = (arr, col) => Math.min(...getColumn(arr, col));
const getMaxValue = (arr, col) => Math.max(...getColumn(arr, col));

const minValueIndex = (arr, col) => getColumn(arr, col).indexOf(getMinValue(arr, col));
const maxValueIndex = (arr, col) => getColumn(arr, col).indexOf(getMaxValue(arr, col));

console.log('minValue: ', getMinValue(arr, 0)); // Calculated
console.log('maxValue: ',getMaxValue(arr, 0)); // Cached

console.log('minValueIndex: ', minValueIndex(arr, 0)); // Cached
console.log('maxValueIndex: ', maxValueIndex(arr, 0)); // Cached
.as-console-wrapper {min-height: 100% !important; top: 0;}
...