Javascript - Binary Search + HashMap
Code\n\n/**\n * @param {number[]} nums\n * @param {number[]} changeIndices\n * @return {number}\n */\nvar earliestSecondToMarkIndices = function (nums, changeIn
# Code\n```\n/**\n * @param {number[]} nums\n * @param {number[]} changeIndices\n * @return {number}\n */\nvar earliestSecondToMarkIndices = function (nums, changeIndices) {\n  const m = changeIndices.length;\n  let ans = -1;\n  let low = 1;\n  let high = m;\n\n  while (low <= high) {\n    const mid = Math.floor((low + high) / 2);\n    if (isOk(nums, changeIndices, mid, m)) {\n      ans = mid;\n      high = mid - 1;\n    } else {\n      low = mid + 1;\n    }\n  }\n\n  return ans;\n};\n\nfunction isOk(nums, changeIndices, mid, m) {\n  const last = new Map();\n  for (let i = 0; i < mid; i++) {\n    const idx = changeIndices[i];\n    last.set(idx, i);\n  }\n  if (last.size !== nums.length) return false;\n\n  let count = 0;\n  for (let i = 0; i < m; i++) {\n    const idx = changeIndices[i];\n    if (i === last.get(idx)) {\n      if (count < nums[idx - 1]) return false;\n      count -= nums[idx - 1];\n    } else {\n      count++;\n    }\n  }\n\n  return true;\n}\n\n```
['Hash Table', 'Binary Search', 'JavaScript']