From 0ea4c036ccaf9119e95321cad9723468e87b52d4 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 10 Nov 2024 13:05:33 +0800 Subject: [PATCH] Add solution and test-cases for problem 3097 --- .../README.md | 43 +++++++++++----- .../Solution.go | 51 ++++++++++++++++++- .../Solution_test.go | 21 ++++---- 3 files changed, 91 insertions(+), 24 deletions(-) diff --git a/leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/README.md b/leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/README.md index d6f546b15..6cdfa04d0 100755 --- a/leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/README.md +++ b/leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/Solution.go b/leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/Solution.go index d115ccf5e..d5537dce9 100644 --- a/leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/Solution.go +++ b/leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/Solution.go @@ -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 } diff --git a/leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/Solution_test.go b/leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/Solution_test.go index 14ff50eb4..2acca2243 100644 --- a/leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/Solution_test.go +++ b/leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/Solution_test.go @@ -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() { }