Check if Array Can Be Made Strictly Increasing by Subtracting Primes
You are given a 0-indexed integer array 'nums' of length 'n'.
You can perform the following operation as many times as you want:
Pick an index 'i' that you haven’t picked before, and pick a prime 'p' strictly less than 'nums[i]', then subtract 'p' from 'nums[i]'.
Return 'true' if you can make 'nums' a strictly increasing array using the above operation and 'false' otherwise.
Example:
Input: nums = [5,1,3]
Output: true
Explanation:
- Pick index i = 1 and subtract prime p = 2 from nums[1]. Now nums = [5, -1, 3].
- Pick index i = 1 and subtract prime p = 2 from nums[1]. Now nums = [5, -3, 3].
- Pick index i = 2 and subtract prime p = 2 from nums[2]. Now nums = [5, -3, 1].
- Pick index i = 2 and subtract prime p = 2 from nums[2]. Now nums = [5, -3, -1].
The array is now strictly increasing.
C++ Code using Greedy Algorithm:
bool isStrictlyIncreasing(vector<int>& nums) {
int n = nums.size();
int minVal = INT_MAX;
for (int i = 0; i < n; i++) {
if (nums[i] > minVal) {
return false;
}
minVal = min(minVal, nums[i]);
}
return true;
}
bool canMakeIncreasing(vector<int>& nums) {
int n = nums.size();
vector<bool> isPrime(100001, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i * i <= 100000; i++) {
if (isPrime[i]) {
for (int j = i * i; j <= 100000; j += i) {
isPrime[j] = false;
}
}
}
for (int i = 0; i < n; i++) {
if (i > 0 && nums[i] <= nums[i - 1]) {
int target = nums[i - 1] + 1;
int j = i;
while (j < n && nums[j] < target) {
j++;
}
if (j == n || nums[j] != target) {
return false;
}
i = j;
}
int p = 2;
while (p < nums[i]) {
if (isPrime[p] && nums[i] - p > 0) {
nums[i] -= p;
break;
}
p++;
}
}
return isStrictlyIncreasing(nums);
}
Explanation:
-
isStrictlyIncreasingFunction: This function checks if the given array is already strictly increasing. It iterates through the array, comparing each element with the minimum value encountered so far. If any element is greater than the minimum value, it returns 'false'; otherwise, it returns 'true'. -
canMakeIncreasingFunction:- Prime Number Sieve: The code first uses the Sieve of Eratosthenes to generate an array of prime numbers up to 100000. This array is used to efficiently determine if a number is prime.
- Greedy Approach: For each element in the array, the code performs the following:
- Check for Decreasing Order: If the current element is less than or equal to the previous element, it needs to be increased. We find the first element after the current position which is equal to the previous element + 1. If this element is not found, the array cannot be made strictly increasing, so we return 'false'.
- Subtract Prime Number: If the current element is already greater than the previous element, we try to find a prime number smaller than the current element to subtract from it. We iterate through primes starting from 2 and subtract the first suitable prime we find. If no suitable prime is found, the array cannot be made strictly increasing, and we return 'false'.
- Final Check: After processing all elements, the code calls
isStrictlyIncreasingto check if the resulting array is strictly increasing. If it is, the function returns 'true'; otherwise, it returns 'false'.
The code uses a greedy approach by trying to maximize the element value at each step. The prime number sieve is used to efficiently find suitable prime numbers to subtract. The code works by repeatedly checking if the array is strictly increasing after each operation and returns 'false' as soon as it detects that the array cannot be made strictly increasing.
Overall: This solution effectively determines whether the given array can be transformed into a strictly increasing array by subtracting prime numbers using a greedy strategy and efficient prime number checking.
原文地址: https://www.cveoy.top/t/topic/lRgS 著作权归作者所有。请勿转载和采集!