You are viewing a single comment's thread. Return to all comments →
Hey this is my solution with Js
function runningMedian(a) { let arr = a.map(el => el).sort((a, b) => a - b) let length = arr.length let isOdd = false let mid = [] let half = length/2 const result = [] if(length % 2 !== 0){ isOdd = true mid = [Math.ceil(half)-1] } else { mid = [(half - 1), half] } while(arr.length && a.length){ let pop = arr.indexOf(a.pop()) if(isOdd){ result.unshift(arr[mid[0]]) mid.unshift(mid[0]-1) arr.splice(pop, 1) isOdd = false } else{ let media = (arr[mid[0]] + arr[mid[1]]) / 2 result.unshift(media) mid.pop() arr.splice(pop, 1) isOdd = true } } return result }
Seems like cookies are disabled on this browser, please enable them to open this website
Find the Running Median
You are viewing a single comment's thread. Return to all comments →
Hey this is my solution with Js