Skip to content

Instantly share code, notes, and snippets.

@technikhil314
Created April 23, 2022 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technikhil314/6e949d9aa9acf14f761169c8d40f952f to your computer and use it in GitHub Desktop.
Save technikhil314/6e949d9aa9acf14f761169c8d40f952f to your computer and use it in GitHub Desktop.
Amazon number of items in container
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