Created
November 26, 2019 17:35
-
-
Save bendozy/a918cc37747ce44d8bc92d0fe443812f to your computer and use it in GitHub Desktop.
How to get a contiguos subarray
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
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