Skip to content

Instantly share code, notes, and snippets.

@mgudesblatart
Last active May 7, 2019 15:48
Show Gist options
  • Save mgudesblatart/d6278cbd6e083beed344cd371808500a to your computer and use it in GitHub Desktop.
Save mgudesblatart/d6278cbd6e083beed344cd371808500a to your computer and use it in GitHub Desktop.
Daily Coding Problem #2
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]))
@mgudesblatart
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment