Description
Solutions
Get Minimum Operations
🤘 INTERN📚RELATED PROBLEMS
Given two arrays change
and arr
that consist of n
and m
integers respectively.
In the i
th operation, one of the two operations can be performed:
arr
by 1
or do nothing.change[i] > 0
and arr[change[i]] = 0
, it can be changed to NULL
.
Assume indexing starts from 1
, find the minimum number of operations required to change all the elements of the array to NULL
or report -1
if it is not possible.
Function Description
Complete the function getMinOperations
in the editor below.
getMinOperations
has the following parameter(s):
int change[n]
: an array of integersint arr[m]
: an array of integers
Returns
int
: the minimum number of operations required to change all the elements to NULL
, or -1
if it is not possible
Example 1:
Input: change = [0, 1, 0, 2], arr = [1, 1]
Output: 4
Explanation:Considern = 4
andm = 2
change[] = [0, 1, 0, 2]
,arr[] = [1, 1]
In the first operation, arr[1]
can be decremented. The array becomes[0, 1]
.In the second operation, since change[2] = 1
andarr[1] = 0
, the first element can be changed toNULL
. The array becomes[NULL, 1]
.In the third operation, arr[2]
can be decremented. The array becomes[NULL, 0]
.In the fourth operation, since This is one optimal path. Return the number of operations,change[4] = 2
andarr[2] = 0
, the second element can be changed toNULL
. The array becomes[NULL, NULL]
.4
.
Constraints:
:o

Related Problems
Testcase
Result
Case 1
input:
output: