Last active
May 7, 2019 15:48
-
-
Save mgudesblatart/d6278cbd6e083beed344cd371808500a to your computer and use it in GitHub Desktop.
Daily Coding Problem #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wind(array){ | |
let t = performance.now(); | |
let left=null, right = null; | |
let s = [...array].sort(); | |
for(let [i,v] of array.entries()){ | |
if(v != s[i] && left == null){ | |
left = i; | |
}else if (v != s[i]){ | |
right = i; | |
} | |
} | |
let t2 = performance.now(); | |
let time = t2 - t; | |
return {left,right, time} | |
} | |
console.log(wind([3,7,5,6,9])) | |
function wind2(array){ | |
let t = performance.now(); | |
let left = null,right = null; | |
let n = array.length; | |
let max_seen = -Number(Infinity), min_seen = Number(Infinity); | |
for(let [i,v] of array.entries()){ | |
max_seen = Math.max(max_seen, v); | |
if(v < max_seen){ | |
right = i; | |
} | |
} | |
for(let i of [...Array(n).keys()].reverse()){ | |
min_seen = Math.min(min_seen, array[i]); | |
if(array[i] > min_seen){ | |
left = i; | |
} | |
} | |
let t2 = performance.now(); | |
let time = t2 - t; | |
return {left,right, time} | |
} | |
console.log(wind2([3,7,5,6,9])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did not solve. I have no idea how to even translate the written expectations to these solutions. But! I did translate python code to JS, so I think at least now I understand python funcs and js funcs a little better.