You are given a 0-indexed array nums and a 0-indexed array
queries.
You can do the following operation at the beginning at most once :
Replace nums with a subsequence of nums.
We start processing queries in the given order; for each query, we do the following:
If the first and the last element of nums is less thanqueries[i], the processing of queries ends.
Otherwise, we choose either the first or the last element of nums if it is greater than or equal toqueries[i], and we remove the chosen element from nums.
Return themaximum number of queries that can be processed by doing the operation optimally.
Input: nums =[1,2,3,4,5], queries =[1,2,3,4,6]Output: 4Explanation: We don't do any operation and process the queries as follows:1- We choose and remove nums[0] since 1<=1, then nums becomes [2,3,4,5].2- We choose and remove nums[0] since 2<=2, then nums becomes [3,4,5].3- We choose and remove nums[0] since 3<=3, then nums becomes [4,5].4- We choose and remove nums[0] since 4<=4, then nums becomes [5].5- We can not choose any elements from nums since they are not greater than or equal to 5.Hence, the answer is4.It can be shown that we can't process more than 4 queries.
Example 2:
1
2
3
4
5
6
7
8
Input: nums =[2,3,2], queries =[2,2,3]Output: 3Explanation: We don't do any operation and process the queries as follows:1- We choose and remove nums[0] since 2<=2, then nums becomes [3,2].2- We choose and remove nums[1] since 2<=2, then nums becomes [3].3- We choose and remove nums[0] since 3<=3, then nums becomes [].Hence, the answer is3.It can be shown that we can't process more than 3 queries.
Example 3:
1
2
3
4
5
6
7
8
9
Input: nums =[3,4,3], queries =[4,3,2]Output: 2Explanation: First we replace nums with the subsequence of nums [4,3].Then we can process the queries as follows:1- We choose and remove nums[0] since 4<=4, then nums becomes [3].2- We choose and remove nums[0] since 3<=3, then nums becomes [].3- We can not process any more queries since nums is empty.Hence, the answer is2.It can be shown that we can't process more than 2 queries.
We can remove a prefix and/or suffix of the array at most once (by picking a subsequence), and then process queries greedily from both ends. The optimal solution is to try all possible subarrays (as subsequences) and simulate the process for each, keeping the maximum number of queries processed.
classSolution {
public:int maxQueries(vector<int>& nums, vector<int>& queries) {
int n = nums.size(), m = queries.size(), ans =0;
for (int l =0; l < n; ++l) {
for (int r = l; r < n; ++r) {
int i = l, j = r, q =0;
while (i <= j && q < m) {
if (nums[i] < queries[q] && nums[j] < queries[q]) break;
if (nums[i] >= queries[q]) i++;
else j--;
q++;
}
ans = max(ans, q);
}
}
return ans;
}
};
funcmaxQueries(nums []int, queries []int) int {
n, m, ans:= len(nums), len(queries), 0forl:=0; l < n; l++ {
forr:=l; r < n; r++ {
i, j, q:=l, r, 0fori<=j&&q < m {
ifnums[i] < queries[q] &&nums[j] < queries[q] {
break }
ifnums[i] >=queries[q] {
i++ } else {
j-- }
q++ }
ifq > ans {
ans = q }
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
publicintmaxQueries(int[] nums, int[] queries) {
int n = nums.length, m = queries.length, ans = 0;
for (int l = 0; l < n; l++) {
for (int r = l; r < n; r++) {
int i = l, j = r, q = 0;
while (i <= j && q < m) {
if (nums[i]< queries[q]&& nums[j]< queries[q]) break;
if (nums[i]>= queries[q]) i++;
else j--;
q++;
}
ans = Math.max(ans, q);
}
}
return ans;
}
}
classSolution {
funmaxQueries(nums: IntArray, queries: IntArray): Int {
val n = nums.size
val m = queries.size
var ans = 0for (l in0 until n) {
for (r in l until n) {
var i = l
var j = r
var q = 0while (i <= j && q < m) {
if (nums[i] < queries[q] && nums[j] < queries[q]) breakif (nums[i] >= queries[q]) i++else j-- q++ }
ans = maxOf(ans, q)
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defmaxQueries(self, nums: list[int], queries: list[int]) -> int:
n, m, ans = len(nums), len(queries), 0for l in range(n):
for r in range(l, n):
i, j, q = l, r, 0while i <= j and q < m:
if nums[i] < queries[q] and nums[j] < queries[q]:
breakif nums[i] >= queries[q]:
i +=1else:
j -=1 q +=1 ans = max(ans, q)
return ans
impl Solution {
pubfnmax_queries(nums: Vec<i32>, queries: Vec<i32>) -> i32 {
let n = nums.len();
let m = queries.len();
letmut ans =0;
for l in0..n {
for r in l..n {
let (mut i, mut j, mut q) = (l, r, 0);
while i <= j && q < m {
if nums[i] < queries[q] && nums[j] < queries[q] { break; }
if nums[i] >= queries[q] { i +=1; }
else { j -=1; }
q +=1;
}
ans = ans.max(q);
}
}
ans asi32 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
maxQueries(nums: number[], queries: number[]):number {
constn=nums.length, m=queries.length;
letans=0;
for (letl=0; l<n; l++) {
for (letr=l; r<n; r++) {
leti=l, j=r, q=0;
while (i<=j&&q<m) {
if (nums[i] <queries[q] &&nums[j] <queries[q]) break;
if (nums[i] >=queries[q]) i++;
elsej--;
q++;
}
ans= Math.max(ans, q);
}
}
returnans;
}
}