Skip to content

Add solution and test-cases for problem 2411 #1272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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].
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading