Skip to content

Instantly share code, notes, and snippets.

@julioflima
Last active June 19, 2023 11:28
Show Gist options
  • Save julioflima/690f134e6d04a233245c26f505be45cb to your computer and use it in GitHub Desktop.
Save julioflima/690f134e6d04a233245c26f505be45cb to your computer and use it in GitHub Desktop.
Google Step one
const data = [50, 60, 40, 80, 30, 56, 63, 42, 65, 23];
const findWidestInterval = (data) => {
if (Array.isArray(data) && !!data.length) {
const maxLostInterval = 0;
for (let i = 0; i < data.length - 1; i += 1) {
for (let j = i; j < data.length - 1; j += 1) {
const startPrice = data[i];
const endPrice = data[j];
const lost = startPrice-endPrice;
const interval = j-i;
if(lost < 0 && interval>maxLostInterval) maxLostInterval = interval
}
}
return maxLostInterval;
}
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment