Problema La mediana de los números M se define como la 1) si M es el número medio extraño después de la clasificación en orden 2) si M es incluso el número promedio de los 2 números del medio (de nuevo después de ordenar) Al principio tiene una lista de números vacía. Luego puede agregar o eliminar un número de la lista. Para cada operación de agregar o eliminar, muestra la mediana de los números en la lista.interviewstreet desafío mediana
Ejemplo: Para un conjunto de m = 5 números, {9, 2, 8, 4, 1} la mediana es el tercer número del conjunto ordenado {1, 2, 4, 8, 9} que es 4. De manera similar para el conjunto de m = 4, {5, 2, 10, 4}, la mediana es el promedio del segundo y tercer elemento del conjunto ordenado {2, 4, 5, 10} que es (4 + 5)/2 = 4,5
Mi enfoque Creo que el problema puede resolverse de esta manera .. idea es utilizar el valor de la mediana anterior y el puntero para encontrar nuevo valor de la mediana en lugar de volver a calcular en cada operación de añadir o eliminar.
1) Utilice multisectos que siempre mantienen los elementos en orden y permiten duplicados. En otras palabras, mantener lista ordenada de alguna manera.
2) Si la operación es añadir
2.1) Insert this element into set and then calculate the median
2.2) if the size of set is 1 then first element will be the median
2.3) if the size of set is even, then
if new element is larger then prev median, new median will be avg of prev median
and the next element in set.
else new median will be avg of prev median and previous of prev element in the set.
2.4) if the size is odd, then
if new element is larger then prev median
if also less then 2nd element of prev median (2nd element used to calculate avg
of prev median) then this new element to be added will be new median
else median will be 2nd element use to calculate the avg during last iteration prev
median.
else
new median will be previous of prev median element in the set
3) Si la operación es eliminar
3.1) First calculate the new median
3.2) If the size of set is 0 can't remove
3.3) If the size is 1 if the first element is the element to be removed, remove it else can't remove.
3.4) If the size of set is even, then
if the element to be deleted is greater than or equal to 2nd element of prev median, then
1st element of prev median will be new median
else 2nd element of prev median will be the new median
3.5) If the size of set is odd, then
if the element to be deleted is the prev median then find the avg of its prev and next element.
else if the element to be deleted is greater then prev median, new median will be avg of prev median and previous to prev median
else median will be avg of prev median and next element to prev median.
3.6) Remove the element.
Aquí está el código de trabajo ... http://justprogrammng.blogspot.com/2012/06/interviewstreet-median-challenge.html. ¿Cuáles son sus puntos de vista sobre este enfoque?
Su código está haciendo cosas raras cuando quito la mediana, por ejemplo: "4 a 1 a 1 a 1 r 1 " – ffao
gracias ffao ... eso fue un pequeño error, ahora corregido. Pero todavía hay un problema que no puedo localizar ... – sachin
% g no se comporta correctamente inténtalo con valores mayores de mediana yx se imprimirá en notación científica – amitkarmakar