[Javascript] Binary Search
Solution: Binary Search\n\nBinary search for the earliest second s.\nTo check if it\'s possible to mark all indices in s seconds,\n   The idea is to delay the "
**Solution: Binary Search**\n\nBinary search for the earliest second `s`.\nTo check if it\'s possible to mark all indices in `s` seconds,\n*   The idea is to delay the "marking" of numbers as much as possible.\n*   The latest point we can mark an index is the last occurance of it in `changeIndices` (only consider the prefix of `changeIndices` up to index `s`).\n*   Before the latest point, store up as many decrement operations as possible.\n*   When the time comes to mark an index, check whether we have enough decrement operations to use on `nums[changeIndices[i]]`.\n*   If the decrement operations are not enough, it\'s not possible - return false.\n*   If they are enough, subtract `nums[changeIndices[i]]` from `operations`.\n\n`n = length of nums`, `m = length of changeIndices`\nTime Complexity: `O((n + m) * log(m))`\nSpace Complexity: `O(n + m)`\n```\nvar earliestSecondToMarkIndices = function(nums, changeIndices) {\n  changeIndices = changeIndices.map((index) => index - 1);\n  let n = nums.length;\n  let low = 0, high = changeIndices.length - 1;\n  while (low < high) {\n    let mid = Math.floor((low + high) / 2);\n    if (isPossible(mid)) high = mid;\n    else low = mid + 1;\n  }\n  return isPossible(low) ? low + 1 : -1;\n  \n  function isPossible(s) {\n    let last = Array(n).fill(-1);\n    for (let i = 0; i <= s; i++) {\n      last[changeIndices[i]] = i;\n    }\n    let marked = 0, operations = 0;\n    for (let i = 0; i <= s; i++) {\n      if (i === last[changeIndices[i]]) {\n        if (nums[changeIndices[i]] > operations) return false;\n        operations -= nums[changeIndices[i]];\n        marked++;\n      } else {\n        operations++;\n      }\n    }\n    return marked === n;\n  }\n};\n```
['JavaScript']