Skip to content

Commit 66cdfeb

Browse files
authored
Merge pull request #984 from 0xff-dev/2419
Add solution and test-cases for problem 2419
2 parents 44e0931 + b98e7f2 commit 66cdfeb

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [2419.Longest Subarray With Maximum Bitwise AND][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 integer array `nums` of size `n`.
5+
6+
Consider a **non-empty** subarray from `nums` that has the **maximum** possible **bitwise AND**.
7+
8+
- In other words, let `k` be the maximum value of the bitwise AND of **any** subarray of `nums`. Then, only subarrays with a bitwise AND equal to `k` should be considered.
9+
Return the length of the longest such subarray.
10+
11+
The bitwise AND of an array is the bitwise AND of all the numbers in it.
12+
13+
A **subarray** is a contiguous sequence of elements within an array.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: nums = [1,2,3,3,2,2]
19+
Output: 2
20+
Explanation:
21+
The maximum possible bitwise AND of a subarray is 3.
22+
The longest subarray with that value is [3,3], so we return 2.
1323
```
1424

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

20-
### 思路1
21-
> ...
22-
Longest Subarray With Maximum Bitwise AND
23-
```go
2427
```
25-
28+
Input: nums = [1,2,3,4]
29+
Output: 1
30+
Explanation:
31+
The maximum possible bitwise AND of a subarray is 4.
32+
The longest subarray with that value is [4], so we return 1.
33+
```
2634

2735
## 结语
2836

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
m := 0
5+
l := 0
6+
ans := 0
7+
for _, n := range nums {
8+
if n == m {
9+
l++
10+
ans = max(ans, l)
11+
continue
12+
}
13+
if n > m {
14+
m = n
15+
l = 1
16+
ans = 1
17+
continue
18+
}
19+
l = 0
20+
}
21+
return ans
522
}

leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/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, 2, 3, 3, 2, 2}, 2},
17+
{"TestCase2", []int{1, 2, 3, 4}, 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)