3097. Shortest Subarray With OR at Least K II #814
-
Topics: You are given an array An array is called special if the bitwise Return the length of the shortest special non-empty subarray1 of Example 1:
Example 2:
Example 3:
Constraints:
Hint:
Footnotes
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We can use a sliding window approach combined with bit manipulation to keep track of the OR of elements in the window. Plan:
Steps:
Let's implement this solution in PHP: 3097. Shortest Subarray With OR at Least K II <?php
class Solution {
const K_MAX_BIT = 30; // Maximum bit position we will check
/**
* @param Integer[] $nums
* @param Integer $k
* @return Integer
*/
function minimumSubarrayLength($nums, $k) {
$n = count($nums);
$ans = $n + 1;
$ors = 0;
$count = array_fill(0, self::K_MAX_BIT + 1, 0); // Array to keep track of bit counts
$l = 0;
for ($r = 0; $r < $n; $r++) {
$ors = $this->orNum($ors, $nums[$r], $count);
// Shrink window from the left as long as ors >= k
while ($ors >= $k && $l <= $r) {
$ans = min($ans, $r - $l + 1);
$ors = $this->undoOrNum($ors, $nums[$l], $count);
$l++;
}
}
return ($ans == $n + 1) ? -1 : $ans;
}
/**
* @param $ors
* @param $num
* @param $count
* @return int
*/
private function orNum($ors, $num, &$count) {
// Update the ors value and count bits that are set
for ($i = 0; $i < self::K_MAX_BIT; $i++) {
if (($num >> $i) & 1) { // Check if the i-th bit is set
$count[$i]++;
if ($count[$i] == 1) { // Only add to ors if this bit is newly set in the window
$ors += (1 << $i); // Add this bit to the cumulative OR
}
}
}
return $ors;
}
/**
* @param $ors
* @param $num
* @param $count
* @return int
*/
private function undoOrNum($ors, $num, &$count) {
// Reverse the update on ors and count bits that are reset
for ($i = 0; $i < self::K_MAX_BIT; $i++) {
if (($num >> $i) & 1) { // Check if the i-th bit is set
$count[$i]--;
if ($count[$i] == 0) { // Only remove from ors if this bit is no longer set in the window
$ors -= (1 << $i); // Remove this bit from the cumulative OR
}
}
}
return $ors;
}
}
// Example usage
$solution = new Solution();
$nums1 = [1, 2, 3];
$k1 = 2;
echo $solution->minimumSubarrayLength($nums1, $k1) . "\n"; // Output: 1
$nums2 = [2, 1, 8];
$k2 = 10;
echo $solution->minimumSubarrayLength($nums2, $k2) . "\n"; // Output: 3
$nums3 = [1, 2];
$k3 = 0;
echo $solution->minimumSubarrayLength($nums3, $k3) . "\n"; // Output: 1
?> Explanation:
4Time Complexity:
|
Beta Was this translation helpful? Give feedback.
We can use a sliding window approach combined with bit manipulation to keep track of the OR of elements in the window.
Plan:
1
, it cannot be unset). This means as we extend the window, the OR value only increases or stays the same.Steps: