3976. Construct Uniform Parity Array II
class Solution {
public boolean uniformArray(int[] nums1) {
return canMakeAll(nums1, 0) || canMakeAll(nums1, 1);
}
private boolean canMakeAll(int[] nums, int targetParity) {
int[] globalMin = {Integer.MAX_VALUE, Integer.MAX_VALUE};
for (int x : nums) {
globalMin[x % 2] = Math.min(globalMin[x % 2], x);
}
for (int x : nums) {
int curParity = x % 2;
if (curParity == targetParity) {
continue;
}
int neededJParity = (targetParity + curParity) % 2;
if (globalMin[neededJParity] >= x) {
return false; // no valid j
}
}
return true;
}
}
class Solution:
def uniformArray(self, nums1):
return self.canMakeAll(nums1, 0) or self.canMakeAll(nums1, 1)
def canMakeAll(self, nums, targetParity):
globalMin = [float('inf'), float('inf')]
for x in nums:
globalMin[x % 2] = min(globalMin[x % 2], x)
for x in nums:
curParity = x % 2
if curParity == targetParity:
continue
neededJParity = (targetParity + curParity) % 2
if globalMin[neededJParity] >= x:
return False
return True
class Solution {
public:
bool uniformArray(vector<int>& nums1) {
return canMakeAll(nums1, 0) || canMakeAll(nums1, 1);
}
private:
bool canMakeAll(vector<int>& nums, int targetParity) {
int globalMin[2] = {INT_MAX, INT_MAX};
for (int x : nums) {
globalMin[x % 2] = min(globalMin[x % 2], x);
}
for (int x : nums) {
int curParity = x % 2;
if (curParity == targetParity)
continue;
int neededJParity = (targetParity + curParity) % 2;
if (globalMin[neededJParity] >= x)
return false;
}
return true;
}
};
class Solution {
uniformArray(nums1) {
return this.canMakeAll(nums1, 0) || this.canMakeAll(nums1, 1);
}
canMakeAll(nums, targetParity) {
const globalMin = [Infinity, Infinity];
for (const x of nums) {
globalMin[x % 2] = Math.min(globalMin[x % 2], x);
}
for (const x of nums) {
const curParity = x % 2;
if (curParity === targetParity)
continue;
const neededJParity = (targetParity + curParity) % 2;
if (globalMin[neededJParity] >= x)
return false;
}
return true;
}
}