From a4c494137fbcd0ddaf7bd661a344b8996081386c Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 18 Mar 2025 22:28:42 +0800 Subject: [PATCH] Add solution and test-cases for problem 2401 --- .../2401.Longest-Nice-Subarray/README.md | 35 +++++++++++-------- .../2401.Longest-Nice-Subarray/Solution.go | 28 +++++++++++++-- .../Solution_test.go | 13 ++++--- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/leetcode/2401-2500/2401.Longest-Nice-Subarray/README.md b/leetcode/2401-2500/2401.Longest-Nice-Subarray/README.md index dfcb6ca72..6a5af6a04 100755 --- a/leetcode/2401-2500/2401.Longest-Nice-Subarray/README.md +++ b/leetcode/2401-2500/2401.Longest-Nice-Subarray/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/2401-2500/2401.Longest-Nice-Subarray/Solution.go b/leetcode/2401-2500/2401.Longest-Nice-Subarray/Solution.go index d115ccf5e..b7083b8ef 100644 --- a/leetcode/2401-2500/2401.Longest-Nice-Subarray/Solution.go +++ b/leetcode/2401-2500/2401.Longest-Nice-Subarray/Solution.go @@ -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 } diff --git a/leetcode/2401-2500/2401.Longest-Nice-Subarray/Solution_test.go b/leetcode/2401-2500/2401.Longest-Nice-Subarray/Solution_test.go index 14ff50eb4..9d562886b 100644 --- a/leetcode/2401-2500/2401.Longest-Nice-Subarray/Solution_test.go +++ b/leetcode/2401-2500/2401.Longest-Nice-Subarray/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }