Skip to content

Commit 77e3030

Browse files
authored
Merge pull request #1027 from 0xff-dev/3097
Add solution and test-cases for problem 3097
2 parents 873b996 + 0ea4c03 commit 77e3030

File tree

3 files changed

+91
-24
lines changed

3 files changed

+91
-24
lines changed

leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/README.md

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
# [3097.Shortest Subarray With OR at Least K II][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 an array `nums` of **non-negative** integers and an integer `k`.
5+
6+
An array is called **special** if the bitwise `OR` of all of its elements is **at least** `k`.
7+
8+
Return the length of the **shortest special non-empty** subarray of `nums`, or return `-1` if no special subarray exists.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [1,2,3], k = 2
14+
15+
Output: 1
16+
17+
Explanation:
18+
19+
The subarray [3] has OR value of 3. Hence, we return 1.
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
23+
24+
```
25+
Input: nums = [2,1,8], k = 10
1726
18-
## 题解
27+
Output: 3
28+
29+
Explanation:
30+
31+
The subarray [2,1,8] has OR value of 11. Hence, we return 3.
32+
```
33+
34+
**Example 3:**
1935

20-
### 思路1
21-
> ...
22-
Shortest Subarray With OR at Least K II
23-
```go
2436
```
37+
Input: nums = [1,2], k = 0
2538
39+
Output: 1
40+
41+
Explanation:
42+
43+
The subarray [1] has OR value of 1. Hence, we return 1.
44+
```
2645

2746
## 结语
2847

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) int {
4+
// 统计一个范围的1的位置的个数, 直接ok操作,没有个数统计,应该是没法滑动窗口,我没想到怎么滑动
5+
// 转成数字看是否比k大
6+
bitCount := [32]int{}
7+
var (
8+
setBit func(int, int)
9+
toNumber func() int
10+
)
11+
setBit = func(n, del int) {
12+
one := 1
13+
for i := 0; i < 32; i++ {
14+
if n&one == one {
15+
bitCount[i] += del
16+
}
17+
one <<= 1
18+
}
19+
}
20+
toNumber = func() int {
21+
res := 0
22+
cur := 1
23+
for i := 0; i < 32; i++ {
24+
if bitCount[i] > 0 {
25+
res += cur
26+
}
27+
cur <<= 1
28+
}
29+
return res
30+
}
31+
start, end := 0, 0
32+
ans := -1
33+
for ; end < len(nums); end++ {
34+
setBit(nums[end], 1)
35+
r := toNumber()
36+
if r < k {
37+
continue
38+
}
39+
for ; start < end; start++ {
40+
setBit(nums[start], -1)
41+
if toNumber() < k {
42+
setBit(nums[start], 1)
43+
break
44+
}
45+
}
46+
tmpL := end - start + 1
47+
if ans == -1 || tmpL < ans {
48+
ans = tmpL
49+
}
50+
}
51+
return ans
552
}

leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 2, 3}, 2, 1},
18+
{"TestCase2", []int{2, 1, 8}, 10, 3},
19+
{"TestCase3", []int{1, 2}, 0, 1},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.nums, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.nums, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)