diff options
author | Jack Sangdahl <[email protected]> | 2025-07-28 20:58:11 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-07-28 20:58:11 -0600 |
commit | 64ccda3aff7e186e03bdc963a8ab110d2bc01e19 (patch) | |
tree | d3aba8d067fefe1072a97fc774759a73e7a03e2d | |
parent | 59613ec9d3a899b43dd9c788cb1c0d14719eff76 (diff) | |
download | leetcode-64ccda3aff7e186e03bdc963a8ab110d2bc01e19.tar.gz leetcode-64ccda3aff7e186e03bdc963a8ab110d2bc01e19.zip |
2411. smallest subarrays with maximum bitwise or
-rw-r--r-- | smallest_subarr_max_or.c | 41 | ||||
-rw-r--r-- | smallest_subarr_max_or.py | 35 |
2 files changed, 76 insertions, 0 deletions
diff --git a/smallest_subarr_max_or.c b/smallest_subarr_max_or.c new file mode 100644 index 0000000..99c4a1f --- /dev/null +++ b/smallest_subarr_max_or.c @@ -0,0 +1,41 @@ +// 2411. Smallest Subarrays With Maximum Bitwise OR +#include "leetcode.h" + +/* + * you are given a 0-indexed array 'nums' of length 'n', consisting of + * non-negative integers. for each index 'i' from 0 to 'n - 1', you must + * determine the size of the minimum sized non-empty subarray of 'nums' starting + * at 'i' that has the maximum possible bitwise or. the bitwosee or of an array + * is the bitwise or of all the numbers in it. return an integer array 'answer' + * of size 'n' where 'answer[i]' is the length of the minimum sized subarray + * starting at 'i' with maximum bitwise or. a subarray is a contiguous non-empty + * sequence of elements within an array. + */ + +int *smallestSubarrays(int *nums, int numsSize, int *returnSize) { + int prev[30], *ans = (int *)malloc(numsSize * sizeof(int)); + memset(ans, 1, numsSize * sizeof(int)); + *returnSize = numsSize; + for (int i = numsSize - 1; i >= 0; --i) + for (int j = 0; j < 30; ++j) { + if (nums[i] & (1 << j)) + prev[j] = i; + ans[i] = fmax(ans[i], prev[j] - i + 1); + } + return ans; +} + +int main() { + int n1[] = {1, 0, 2, 1, 3}, rs1; + int n2[] = {1, 2}, rs2; + int *sb1 = smallestSubarrays(n1, ARRAY_SIZE(n1), &rs1); + int *sb2 = smallestSubarrays(n2, ARRAY_SIZE(n2), &rs2); + for (int i = 0; i < rs1; i++) + printf("%d ", sb1[i]); // expect: 3 3 2 2 1 + printf("\n"); + for (int i = 0; i < rs2; i++) + printf("%d ", sb2[i]); // expect: 2 1 + printf("\n"); + free(sb1); + free(sb2); +} diff --git a/smallest_subarr_max_or.py b/smallest_subarr_max_or.py new file mode 100644 index 0000000..a0506fc --- /dev/null +++ b/smallest_subarr_max_or.py @@ -0,0 +1,35 @@ +# 2411. Smallest Subarrays With Maximum Bitwise OR + +""" +you are given a 0-indexed array 'nums' of length 'n', consisting of +non-negative integers. for each index 'i' from 0 to 'n - 1', you must +determine the size of the minimum sized non-empty subarray of 'nums' starting +at 'i' that has the maximum possible bitwise or. the bitwosee or of an array +is the bitwise or of all the numbers in it. return an integer array 'answer' +of size 'n' where 'answer[i]' is the length of the minimum sized subarray +starting at 'i' with maximum bitwise or. a subarray is a contiguous non-empty +sequence of elements within an array. +""" + + +class Solution(object): + def smallestSubarrays(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + prev = [0] * 32 + n = len(nums) + ans = [0] * n + for i in range(n - 1, -1, -1): + for j in range(32): + if nums[i] & (1 << j): + prev[j] = i + ans[i] = max(1, max(prev) - i + 1) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.smallestSubarrays(nums=[1, 0, 2, 1, 3])) + print(obj.smallestSubarrays(nums=[1, 2])) |