Skip to content

Instantly share code, notes, and snippets.

@germanescobar
Created March 5, 2022 00:33
Show Gist options
  • Save germanescobar/46a4208c8cf9f4e16d64ac04d0bdae59 to your computer and use it in GitHub Desktop.
Save germanescobar/46a4208c8cf9f4e16d64ac04d0bdae59 to your computer and use it in GitHub Desktop.
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