Created
March 5, 2022 00:33
-
-
Save germanescobar/46a4208c8cf9f4e16d64ac04d0bdae59 to your computer and use it in GitHub Desktop.
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
var platesBetweenCandles = function(s, queries) { | |
const result = [] | |
for (let i=0; i < queries.length; i++) { | |
const query = queries[i] | |
result.push(calculate(s, query[0], query[1])) | |
} | |
return result | |
}; | |
function calculate(s, start, end) { | |
let foundStart = false | |
let foundEnd = false | |
while (start <= end && !(foundStart && foundEnd)) { | |
if (s[start] === "|") { | |
foundStart = true | |
} else { | |
start++ | |
} | |
if (s[end] === "|") { | |
foundEnd = true | |
} else { | |
end-- | |
} | |
} | |
let plates = 0 | |
for (let i=start+1; i < end; i++) { | |
if (s[i] === "*") { | |
plates++ | |
} | |
} | |
return plates | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment