-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution and test-cases for problem 3097
- Loading branch information
Showing
3 changed files
with
91 additions
and
24 deletions.
There are no files selected for viewing
43 changes: 31 additions & 12 deletions
43
leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 49 additions & 2 deletions
51
leetcode/3001-3100/3097.Shortest-Subarray-With-OR-at-Least-K-II/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters