Skip to content

Instantly share code, notes, and snippets.

@bendozy
Created November 26, 2019 17:35
Show Gist options
  • Save bendozy/a918cc37747ce44d8bc92d0fe443812f to your computer and use it in GitHub Desktop.
Save bendozy/a918cc37747ce44d8bc92d0fe443812f to your computer and use it in GitHub Desktop.
How to get a contiguos subarray
const number = 4
const affected = [1, 2]
const poisonous = [3, 4]
function getSubArrayz(n) {
const LEFT = [];
let answer = 0
for(let i = 0; i <= n; i++){
LEFT[i] = 0;
}
console.log('LEFT 1', LEFT)
for(let i = 0; i < poisonous.length; i++) {
if(affected[i]) {
let a = affected[i];
let b = poisonous[i];
let tmp;
console.log('L B', a, b)
if(a > b) {
tmp = a;
a = b;
b = tmp;
}
LEFT[b] = Math.max(LEFT[b], a)
}
}
console.log('LEFT 2', LEFT)
for(let i = 2; i <= n; i++) {
LEFT[i] = Math.max(LEFT[i], LEFT[i - 1]);
}
console.log('LEFT 3', LEFT)
for(let i = 1; i <= n; i++) {
answer += i - LEFT[i];
}
console.log('LEFT 4', LEFT)
return answer;
}
getSubArrayz(number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment