Day 15
Make Array Strictly Increasing
(Leetcode)
Problem Description
Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing.
In one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and do the assignment arr1[i] = arr2[j].
If there is no way to make arr1 strictly increasing, return -1.
Problem Description
Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing.
In one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and do the assignment arr1[i] = arr2[j].
If there is no way to make arr1 strictly increasing, return -1.
arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
Problem Description
Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing.
In one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and do the assignment arr1[i] = arr2[j].
If there is no way to make arr1 strictly increasing, return -1.
arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
arr1 = [1,2,3,6,7]
1 operation
How To approach this problem?
We will first do brute force meaning
try replacing every number from the arr2 check how many operation it will take to make complete array strictly increasing.
| i-1 | i | i+1 |
|---|
How To approach this problem?
We will first do brute force meaning
try replacing every number from the arr2 check how many operation it will take to make complete array strictly increasing.
| i-1 | i | i+1 |
|---|
arr[i] <= arr[i-1]
arr[i] > arr[i-1]
How To approach this problem?
| i-1 | i | i+1 |
|---|
arr[i] <= arr[i-1]
arr[i] > arr[i-1]
It means we have to take an element from arr2 to replace arr1[i]
that element must be greater than the arr[i-1].
Choosing numbers from arr2
arr2 = [a,b,c,d]
a > arr1[i-1]
b > arr1[i-1]
Here we will apply the greedy approach to choose elements from the arr2. Meaning we will choose least greater element.
Choosing numbers from arr2
arr2 = [a,b,c,d]
a > arr1[i-1]
b > arr1[i-1]
Here we will apply the greedy approach to choose elements from the arr2. Meaning we will choose least greater element.
arr1 = 1, 9, 4, 5, 6
arr2 = 2, 8, 12, 13
So we will sort the arr2 and choose the just greater element by applying binary search
How To approach this problem?
| i-1 | i | i+1 |
|---|
arr[i] <= arr[i-1]
arr[i] > arr[i-1]
It means we have to take an element from arr2 to replace arr1[i]
that element must be greater than the arr1[i-1].
How To approach this problem?
| i-1 | i | i+1 |
|---|
arr[i] <= arr[i-1]
arr[i] > arr[i-1]
Let it be
How To approach this problem?
| i-1 | i | i+1 |
|---|
arr[i] <= arr[i-1]
arr[i] > arr[i-1]
Let it be
Replace
How To approach this problem?
| i-1 | i | i+1 |
|---|
arr[i] <= arr[i-1]
arr[i] > arr[i-1]
Let it be
Replace
???
How To approach this problem?
| i-1 | i | i+1 |
|---|
arr[i] <= arr[i-1]
arr[i] > arr[i-1]
Let it be
Replace
???
arr1 = 1, 2, 100, 5, 6, 7
arr2 = 3, 101, 102, 103
How To approach this problem?
| i-1 | i | i+1 |
|---|
arr[i] <= arr[i-1]
arr[i] > arr[i-1]
Let it be
Replace
???
arr1 = 1, 2, 100, 5, 6, 7
arr2 = 3, 101, 102, 103
How To approach this problem?
| i-1 | i | i+1 |
|---|
arr[i] <= arr[i-1]
arr[i] > arr[i-1]
Let it be
Replace
Replace
Time consuming
How To approach this problem?
We will apply the same approach but with memoization.
Getting the states of memoization
| L | i | i+1 |
|---|
DP[i][L] = min operation required till ith with left of arr1 as value L
Let's try to form the solution of this approach?
arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
arr1 = 1, 5, 3, 6, 7
arr2 = 1, 2, 3, 4
let arr1[-1] = -inf
hence arr1[0] > arr1[-1]
Let's try to form the solution of this approach?
arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
arr1 = 1, 5, 3, 6, 7
arr2 = 1, 2, 3, 4
hence arr1[1] > arr1[0]
Let's try to form the solution of this approach?
arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
arr1 = 1, 5, 3, 6, 7
arr2 = 1, 2, 3, 4
hence arr1[2] < arr1[1]
Not possible configuration
Let's try to form the solution of this approach?
arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
arr1 = 1, 5, 3, 6, 7
arr2 = 1, 2, 3, 4
hence arr1[2] < arr1[1]
Not possible configuration
arr1 = 1, 2, 3, 6, 7
arr2 = 1, 2, 3, 4
Code
int check(int curr, int left, vector<int>& arr1 ,vector<int>& arr2, vector <unordered_map<int,int> >& dp)
{
if (curr == arr1.size())
return 0;
if (dp[curr].find(left) != dp[curr].end())
return dp[curr][left];
int res1,res2;
if (arr1[curr] > left)
{
res1 = check(curr+1, arr1[curr], arr1, arr2, dp);
}
else
{
res1 = INT_MAX;
}
int rep = upper_bound(arr2.begin(),arr2.end(), left) - arr2.begin();
if (rep == arr2.size())
res2 = INT_MAX;
else
res2 = check(curr+1, arr2[rep], arr1, arr2, dp);
if (res2 == INT_MAX)
dp[curr][left] = res1;
else
dp[curr][left] = min(res1,res2+1);
return dp[curr][left];
}
int makeArrayIncreasing(vector<int>& arr1, vector<int>& arr2) {
sort(arr2.begin(), arr2.end());
vector <unordered_map<int,int> > dp(2001);
int val = check(0, INT_MIN, arr1, arr2, dp);
if (val != INT_MAX)
return val;
else
return -1;
}
Time Complexity
So that's It for Day 15
So that's It for Day 15
Concepts that we have learnt
So that's It for Day 15
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. Dynamic Programming + Greedy
So that's It for Day 15
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. Dynamic Programming + Greedy
2. Use of Brute force with DP.
So that's It for Day 15
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. Dynamic Programming + Greedy
2. Use of Brute force with DP.
Still, Have any Doubts???
Still, Have any Doubts???
Comment it You will get a reply
OR
Send your doubt to email provided in description.