Skip to content

Commit 12c3285

Browse files
authored
Merge pull request #1272 from 0xff-dev/2411
Add solution and test-cases for problem 2411
2 parents 68fadcd + b9d19b9 commit 12c3285

File tree

3 files changed

+94
-23
lines changed

3 files changed

+94
-23
lines changed

leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [2411.Smallest Subarrays With Maximum Bitwise OR][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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**.
5+
6+
- 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`.
7+
8+
The bitwise OR of an array is the bitwise OR of all the numbers in it.
9+
10+
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.
11+
12+
A **subarray** is a contiguous non-empty sequence of elements within an array.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: nums = [1,0,2,1,3]
18+
Output: [3,3,2,2,1]
19+
Explanation:
20+
The maximum possible bitwise OR starting at any index is 3.
21+
- Starting at index 0, the shortest subarray that yields it is [1,0,2].
22+
- Starting at index 1, the shortest subarray that yields the maximum bitwise OR is [0,2,1].
23+
- Starting at index 2, the shortest subarray that yields the maximum bitwise OR is [2,1].
24+
- Starting at index 3, the shortest subarray that yields the maximum bitwise OR is [1,3].
25+
- Starting at index 4, the shortest subarray that yields the maximum bitwise OR is [3].
26+
Therefore, we return [3,3,2,2,1].
1327
```
1428

15-
## 题意
16-
> ...
17-
18-
## 题解
29+
**Example 2:**
1930

20-
### 思路1
21-
> ...
22-
Smallest Subarrays With Maximum Bitwise OR
23-
```go
2431
```
25-
32+
Input: nums = [1,2]
33+
Output: [2,1]
34+
Explanation:
35+
Starting at index 0, the shortest subarray that yields the maximum bitwise OR is of length 2.
36+
Starting at index 1, the shortest subarray that yields the maximum bitwise OR is of length 1.
37+
Therefore, we return [2,1].
38+
```
2639

2740
## 结语
2841

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func oneArray(n int) [32]int {
6+
res := [32]int{}
7+
index := 31
8+
for ; n > 0; index-- {
9+
if n&1 == 1 {
10+
res[index]++
11+
}
12+
n >>= 1
13+
}
14+
return res
15+
}
16+
17+
func Solution(nums []int) []int {
18+
l := len(nums)
19+
ans := make([]int, l)
20+
ans[l-1] = 1
21+
22+
or := make([]int, l)
23+
or[l-1] = nums[l-1]
24+
for i := l - 2; i >= 0; i-- {
25+
or[i] = nums[i] | or[i+1]
26+
}
27+
28+
oneCount := make([][32]int, l)
29+
oneCount[0] = oneArray(nums[0])
30+
for i := 1; i < l; i++ {
31+
cur := oneArray(nums[i])
32+
for j := 0; j < 32; j++ {
33+
oneCount[i][j] = oneCount[i-1][j] + cur[j]
34+
}
35+
}
36+
var ok func(int, int, int) bool
37+
ok = func(start, end, target int) bool {
38+
tmp := oneCount[end]
39+
if start != 0 {
40+
for i := 0; i < 32; i++ {
41+
tmp[i] -= oneCount[start-1][i]
42+
}
43+
}
44+
base := 1
45+
num := 0
46+
for i := 31; i >= 0; i-- {
47+
x := 1
48+
if tmp[i] == 0 {
49+
x = 0
50+
}
51+
num += x * base
52+
base <<= 1
53+
}
54+
return num == target
55+
}
56+
for i := l - 2; i >= 0; i-- {
57+
// i = 3
58+
index := sort.Search(l-i, func(j int) bool {
59+
return ok(i, i+j, or[i])
60+
})
61+
ans[i] = index + 1
62+
}
63+
return ans
564
}

leetcode/2401-2500/2411.Smallest-Subarrays-With-Maximum-Bitwise-OR/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 0, 2, 1, 3}, []int{3, 3, 2, 2, 1}},
17+
{"TestCase2", []int{1, 2}, []int{2, 1}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)