{ "data": { "question": { "questionId": "3289", "questionFrontendId": "3049", "boundTopicId": null, "title": "Earliest Second to Mark Indices II", "titleSlug": "earliest-second-to-mark-indices-ii", "content": "
You are given two 1-indexed integer arrays, nums
and, changeIndices
, having lengths n
and m
, respectively.
Initially, all indices in nums
are unmarked. Your task is to mark all indices in nums
.
In each second, s
, in order from 1
to m
(inclusive), you can perform one of the following operations:
i
in the range [1, n]
and decrement nums[i]
by 1
.nums[changeIndices[s]]
to any non-negative value.i
in the range [1, n]
, where nums[i]
is equal to 0
, and mark index i
.Return an integer denoting the earliest second in the range [1, m]
when all indices in nums
can be marked by choosing operations optimally, or -1
if it is impossible.
\n
Example 1:
\n\n\nInput: nums = [3,2,3], changeIndices = [1,3,2,2,2,2,3]\nOutput: 6\nExplanation: In this example, we have 7 seconds. The following operations can be performed to mark all indices:\nSecond 1: Set nums[changeIndices[1]] to 0. nums becomes [0,2,3].\nSecond 2: Set nums[changeIndices[2]] to 0. nums becomes [0,2,0].\nSecond 3: Set nums[changeIndices[3]] to 0. nums becomes [0,0,0].\nSecond 4: Mark index 1, since nums[1] is equal to 0.\nSecond 5: Mark index 2, since nums[2] is equal to 0.\nSecond 6: Mark index 3, since nums[3] is equal to 0.\nNow all indices have been marked.\nIt can be shown that it is not possible to mark all indices earlier than the 6th second.\nHence, the answer is 6.\n\n\n
Example 2:
\n\n\nInput: nums = [0,0,1,2], changeIndices = [1,2,1,2,1,2,1,2]\nOutput: 7\nExplanation: In this example, we have 8 seconds. The following operations can be performed to mark all indices:\nSecond 1: Mark index 1, since nums[1] is equal to 0.\nSecond 2: Mark index 2, since nums[2] is equal to 0.\nSecond 3: Decrement index 4 by one. nums becomes [0,0,1,1].\nSecond 4: Decrement index 4 by one. nums becomes [0,0,1,0].\nSecond 5: Decrement index 3 by one. nums becomes [0,0,0,0].\nSecond 6: Mark index 3, since nums[3] is equal to 0.\nSecond 7: Mark index 4, since nums[4] is equal to 0.\nNow all indices have been marked.\nIt can be shown that it is not possible to mark all indices earlier than the 7th second.\nHence, the answer is 7.\n\n\n
Example 3:
\n\n\nInput: nums = [1,2,3], changeIndices = [1,2,3]\nOutput: -1\nExplanation: In this example, it can be shown that it is impossible to mark all indices, as we don't have enough seconds. \nHence, the answer is -1.\n\n\n
\n
Constraints:
\n\n1 <= n == nums.length <= 5000
0 <= nums[i] <= 109
1 <= m == changeIndices.length <= 5000
1 <= changeIndices[i] <= n
n
seconds, and at most sum(nums[i]) + n
seconds.",
"We can binary search the earliest second where all indices can be marked.",
"If there is an operation where we change nums[changeIndices[i]]
to a non-negative value, it is best for it to satisfy the following constraints:nums[changeIndices[i]]
should not be equal to 0
.nums[changeIndices[i]]
should be changed to 0
.changeIndices[i]
occurs in changeIndices
.j
, where changeIndices[i]
will be marked. j
is in the range [i + 1, m]
.time_needed = sum(nums[i]) + n
. To check if we can mark all indices at some second x
, we need to make time_needed <= x
, using non-negative change operations as described previously.",
"Using a non-negative change operation on some nums[changeIndices[i]]
that satisfies the constraints described previously reduces time_needed
by nums[changeIndices[i]] - 1
. So, we need to maximize the sum of (nums[changeIndices[i]] - 1)
while ensuring that the non-negative change operations still satisfy the constraints.",
"Maximizing the sum of (nums[changeIndices[i]] - 1)
can be done greedily using a min-priority queue and going in reverse starting from second x
to second 1
, maximizing the sum of the values in the priority queue and ensuring that for every non-negative change operation on nums[changeIndices[i]]
chosen, there is another second j
in the range [i + 1, x]
where changeIndices[i]
can be marked.",
"The answer is the first value of x
in the range [1, m]
where it is possible to make time_needed <= x
, or -1
if there is no such second."
],
"solution": null,
"status": null,
"sampleTestCase": "[3,2,3]\n[1,3,2,2,2,2,3]",
"metaData": "{\n \"name\": \"earliestSecondToMarkIndices\",\n \"params\": [\n {\n \"name\": \"nums\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"integer[]\",\n \"name\": \"changeIndices\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n }\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": true,
"envInfo": "{\"cpp\": [\"C++\", \"Compiled with clang 17
using the latest C++ 20 standard.
Your code is compiled with level two optimization (-O2
). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.
Most standard library headers are already included automatically for your convenience.
\"], \"java\": [\"Java\", \"OpenJDK 21
. Using compile arguments: --enable-preview --release 21
Most standard library headers are already included automatically for your convenience.
\\r\\nIncludes Pair
class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.
Python 2.7.12
.
Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.
\\r\\n\\r\\nFor Map/TreeMap data structure, you may use sortedcontainers library.
\\r\\n\\r\\nNote that Python 2.7 will not be maintained past 2020. For the latest Python, please choose Python3 instead.
\"], \"c\": [\"C\", \"Compiled with gcc 11
using the gnu11 standard.
Your code is compiled with level one optimization (-O2
). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.
Most standard library headers are already included automatically for your convenience.
\\r\\n\\r\\nFor hash table operations, you may use uthash. \\\"uthash.h\\\" is included by default. Below are some examples:
\\r\\n\\r\\n1. Adding an item to a hash.\\r\\n
\\r\\nstruct hash_entry {\\r\\n int id; /* we'll use this field as the key */\\r\\n char name[10];\\r\\n UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n\\r\\n\\r\\n\\r\\n
2. Looking up an item in a hash:\\r\\n
\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n struct hash_entry *s;\\r\\n HASH_FIND_INT(users, &user_id, s);\\r\\n return s;\\r\\n}\\r\\n\\r\\n\\r\\n\\r\\n
3. Deleting an item in a hash:\\r\\n
\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n HASH_DEL(users, user); \\r\\n}\\r\\n\\r\\n\"], \"csharp\": [\"C#\", \"
C# 12 with .NET 8 runtime
\"], \"javascript\": [\"JavaScript\", \"Node.js 20.10.0
.
Your code is run with --harmony
flag, enabling new ES6 features.
lodash.js library is included by default.
\\r\\n\\r\\nFor Priority Queue / Queue data structures, you may use 5.4.0 version of datastructures-js/priority-queue and 4.2.3 version of datastructures-js/queue.
\"], \"ruby\": [\"Ruby\", \"Ruby 3.2
Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms
\"], \"swift\": [\"Swift\", \"Swift 5.9
.
You may use swift-algorithms 1.2.0 and swift-collections 1.0.6.
\"], \"golang\": [\"Go\", \"Go 1.21
Support https://godoc.org/github.com/emirpasic/gods@v1.18.1 library.
\"], \"python3\": [\"Python3\", \"Python 3.11
.
Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.
\\r\\n\\r\\nFor Map/TreeMap data structure, you may use sortedcontainers library.
\"], \"scala\": [\"Scala\", \"Scala 3.3.1
.
Kotlin 1.9.0
.
We are using an experimental compiler provided by JetBrains.
\"], \"rust\": [\"Rust\", \"Rust 1.74.1
Supports rand v0.6\\u00a0from crates.io
\"], \"php\": [\"PHP\", \"PHP 8.2
.
With bcmath module
\"], \"typescript\": [\"Typescript\", \"TypeScript 5.1.6, Node.js 20.10.0
.
Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022
Your code is run with --harmony
flag, enabling new ES2022 features.
lodash.js library is included by default.
\"], \"racket\": [\"Racket\", \"Racket CS v8.11
\\r\\n\\r\\nUsing #lang racket
Required data/gvector data/queue data/order data/heap
automatically for your convenience
Dart 3.2
\\r\\n\\r\\nYour code will be run directly without compiling
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }