diff --git a/leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/README.md b/leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/README.md index 6b276380b..01d3c42f7 100755 --- a/leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/README.md +++ b/leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/README.md @@ -1,28 +1,41 @@ # [2411.Smallest Subarrays With Maximum Bitwise OR][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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` (inclusive) that has the **maximum** possible **bitwise OR**. + +- In other words, let `Bij` be the bitwise OR of the subarray `nums[i...j]`. You need to find the smallest subarray starting at `i`, such that bitwise OR of this subarray is equal to `max(Bik`) where `i <= k <= n - 1`. + +The bitwise 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. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,0,2,1,3] +Output: [3,3,2,2,1] +Explanation: +The maximum possible bitwise OR starting at any index is 3. +- Starting at index 0, the shortest subarray that yields it is [1,0,2]. +- Starting at index 1, the shortest subarray that yields the maximum bitwise OR is [0,2,1]. +- Starting at index 2, the shortest subarray that yields the maximum bitwise OR is [2,1]. +- Starting at index 3, the shortest subarray that yields the maximum bitwise OR is [1,3]. +- Starting at index 4, the shortest subarray that yields the maximum bitwise OR is [3]. +Therefore, we return [3,3,2,2,1]. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Smallest Subarrays With Maximum Bitwise OR -```go ``` - +Input: nums = [1,2] +Output: [2,1] +Explanation: +Starting at index 0, the shortest subarray that yields the maximum bitwise OR is of length 2. +Starting at index 1, the shortest subarray that yields the maximum bitwise OR is of length 1. +Therefore, we return [2,1]. +``` ## 结语 diff --git a/leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/Solution.go b/leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/Solution.go index d115ccf5e..b558a316b 100644 --- a/leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/Solution.go +++ b/leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/Solution.go @@ -1,5 +1,64 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func oneArray(n int) [32]int { + res := [32]int{} + index := 31 + for ; n > 0; index-- { + if n&1 == 1 { + res[index]++ + } + n >>= 1 + } + return res +} + +func Solution(nums []int) []int { + l := len(nums) + ans := make([]int, l) + ans[l-1] = 1 + + or := make([]int, l) + or[l-1] = nums[l-1] + for i := l - 2; i >= 0; i-- { + or[i] = nums[i] | or[i+1] + } + + oneCount := make([][32]int, l) + oneCount[0] = oneArray(nums[0]) + for i := 1; i < l; i++ { + cur := oneArray(nums[i]) + for j := 0; j < 32; j++ { + oneCount[i][j] = oneCount[i-1][j] + cur[j] + } + } + var ok func(int, int, int) bool + ok = func(start, end, target int) bool { + tmp := oneCount[end] + if start != 0 { + for i := 0; i < 32; i++ { + tmp[i] -= oneCount[start-1][i] + } + } + base := 1 + num := 0 + for i := 31; i >= 0; i-- { + x := 1 + if tmp[i] == 0 { + x = 0 + } + num += x * base + base <<= 1 + } + return num == target + } + for i := l - 2; i >= 0; i-- { + // i = 3 + index := sort.Search(l-i, func(j int) bool { + return ok(i, i+j, or[i]) + }) + ans[i] = index + 1 + } + return ans } diff --git a/leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/Solution_test.go b/leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/Solution_test.go index 14ff50eb4..fe25e83c0 100644 --- a/leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/Solution_test.go +++ b/leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 0, 2, 1, 3}, []int{3, 3, 2, 2, 1}}, + {"TestCase2", []int{1, 2}, []int{2, 1}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }