Skip to content

Commit

Permalink
Add solution and test-cases for problem 3097
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Nov 10, 2024
1 parent ffaca5d commit 0ea4c03
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
# [3097.Shortest Subarray With OR at Least K II][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 an array `nums` of **non-negative** integers and an integer `k`.

An array is called **special** if the bitwise `OR` of all of its elements is **at least** `k`.

Return the length of the **shortest special non-empty** subarray of `nums`, or return `-1` if no special subarray exists.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [1,2,3], k = 2
Output: 1
Explanation:
The subarray [3] has OR value of 3. Hence, we return 1.
```

## 题意
> ...
**Example 2:**

```
Input: nums = [2,1,8], k = 10
## 题解
Output: 3
Explanation:
The subarray [2,1,8] has OR value of 11. Hence, we return 3.
```

**Example 3:**

### 思路1
> ...
Shortest Subarray With OR at Least K II
```go
```
Input: nums = [1,2], k = 0
Output: 1
Explanation:
The subarray [1] has OR value of 1. Hence, we return 1.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int, k int) int {
// 统计一个范围的1的位置的个数, 直接ok操作,没有个数统计,应该是没法滑动窗口,我没想到怎么滑动
// 转成数字看是否比k大
bitCount := [32]int{}
var (
setBit func(int, int)
toNumber func() int
)
setBit = func(n, del int) {
one := 1
for i := 0; i < 32; i++ {
if n&one == one {
bitCount[i] += del
}
one <<= 1
}
}
toNumber = func() int {
res := 0
cur := 1
for i := 0; i < 32; i++ {
if bitCount[i] > 0 {
res += cur
}
cur <<= 1
}
return res
}
start, end := 0, 0
ans := -1
for ; end < len(nums); end++ {
setBit(nums[end], 1)
r := toNumber()
if r < k {
continue
}
for ; start < end; start++ {
setBit(nums[start], -1)
if toNumber() < k {
setBit(nums[start], 1)
break
}
}
tmpL := end - start + 1
if ans == -1 || tmpL < ans {
ans = tmpL
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
nums []int
k int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 3}, 2, 1},
{"TestCase2", []int{2, 1, 8}, 10, 3},
{"TestCase3", []int{1, 2}, 0, 1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.nums, c.k)
}
})
}
}

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

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

0 comments on commit 0ea4c03

Please sign in to comment.