Skip to content

Add solution and test-cases for problem 2401 #1153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions leetcode/2401-2500/2401.Longest-Nice-Subarray/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [2401.Longest Nice Subarray][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` consisting of **positive** integers.

We call a subarray of `nums` **nice** if the bitwise **AND** of every pair of elements that are in **different** positions in the subarray is equal to `0`.

Return the length of the **longest** nice subarray.

A **subarray** is a **contiguous** part of an array.

**Note** that subarrays of length `1` are always considered nice.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [1,3,8,48,10]
Output: 3
Explanation: The longest nice subarray is [3,8,48]. This subarray satisfies the conditions:
- 3 AND 8 = 0.
- 3 AND 48 = 0.
- 8 AND 48 = 0.
It can be proven that no longer nice subarray can be obtained, so we return 3.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Longest Nice Subarray
```go
```

Input: nums = [3,1,5,11,13]
Output: 1
Explanation: The length of the longest nice subarray is 1. Any subarray of length 1 can be chosen.
```

## 结语

Expand Down
28 changes: 26 additions & 2 deletions leetcode/2401-2500/2401.Longest-Nice-Subarray/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int) int {
if len(nums) == 1 {
return 1
}

cur := 0
start, end := 0, 0
ans := 0
for ; end < len(nums); end++ {
if cur&nums[end] == 0 {
cur |= nums[end]
continue
}
ans = max(ans, end-start)

for ; start <= end; start++ {
cur ^= nums[start]
if cur&nums[end] == 0 {
start++
cur |= nums[end]
break
}
}
}
ans = max(ans, end-start)
return ans
}
13 changes: 6 additions & 7 deletions leetcode/2401-2500/2401.Longest-Nice-Subarray/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 3, 8, 48, 10}, 3},
{"TestCase2", []int{3, 1, 5, 11, 13}, 1},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

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

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