2576. Find the Maximum Number of Marked Indices
You are given a 0-indexed integer array nums.
Initially, all of the indices are unmarked. You are allowed to make this operation any number of times:
- Pick two different unmarked indices i and j such that 2 * nums[i] <= nums[j], then mark i and j.
Return the maximum possible number of marked indices in nums using the above operation any number of times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public: int maxNumOfMarkedIndices(vector<int>& A) { sort(begin(A), end(A)); multiset<int> free, match; for(auto a : A) free.insert(a); int res = 0; for(int i = A.size() - 1; i >= 0; i--) { if(free.size() and A[i] * 2 <= *prev(end(free))) { free.erase(prev(end(free))); free.erase(free.find(A[i])); match.insert(A[i]); res += 2; } else if(match.size() and A[i] * 2 <= *prev(end(match))) { free.insert(*prev(end(match))); match.erase(prev(end(match))); free.erase(free.find(A[i])); match.insert(A[i]); } } return res; } };
|