Java Clean Solution | Weekly Contest
Complexity\n- Time complexity:O(nlog(n))\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(n)\n Add your space complexity here, e.g. O(n) \n\n
# Complexity\n- Time complexity:$$O(nlog(n))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    private boolean helper(int[] nums, int[] changeIndices, int idx) {\n        boolean[] vis = new boolean[nums.length];\n        int visCnt = 0;\n        int des = 0;\n        for (int i = idx; i >= 0; i--) {\n            int validx = changeIndices[i];\n            if (vis[validx]) {\n                if (des > 0)\n                    des--;\n            } else {\n                vis[validx] = true;\n                visCnt++;\n                des += nums[validx];\n            }\n        }\n        return des == 0 && visCnt == nums.length;\n    }\n\n\n    public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {\n        for (int i = 0; i < changeIndices.length; i++) {\n            changeIndices[i]--;\n        }\n\n        if (!helper(nums, changeIndices, changeIndices.length - 1)) {\n            return -1;\n        }\n\n        int low = 0, high = changeIndices.length - 1;\n        while (low < high) {\n            int mid = (high - low) / 2 + low;\n            if (helper(nums, changeIndices, mid)) {\n                high = mid;\n            } else {\n                low = mid + 1;\n            }\n        }\n        return high + 1;\n    }\n}\n```
['Array', 'Two Pointers', 'Binary Search', 'Greedy', 'Java']