Created
April 23, 2022 02:42
-
-
Save technikhil314/6e949d9aa9acf14f761169c8d40f952f to your computer and use it in GitHub Desktop.
Amazon number of items in container
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
function numberOfItems(s, startIndices, endIndices) { | |
// Write your code here | |
let result = []; | |
const countArr = {} | |
let count = 0 | |
for(let i = s.indexOf("|"); i< s.length; i++) { | |
if(s[i] === "|") { | |
countArr[i] = count; | |
} else { | |
count++; | |
} | |
} | |
for(let i = 0; i < startIndices.length; i++) { | |
let start = startIndices[i] - 1; | |
let end = endIndices[i] - 1; | |
while (s[start] !== '|') start++; | |
while (s[end] !== '|') end--; | |
console.log(start ,end); | |
result[i] = start < end ? countArr[end] - countArr[start] : 0 | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment