From b98e7f277509ec5e53271e617f1398dd960caf8a Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 14 Sep 2024 09:54:16 +0800 Subject: [PATCH] Add solution and test-cases for problem 2419 --- .../README.md | 36 +++++++++++-------- .../Solution.go | 21 +++++++++-- .../Solution_test.go | 13 ++++--- 3 files changed, 47 insertions(+), 23 deletions(-) diff --git a/leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/README.md b/leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/README.md index 4e5d0c596..f26cdeb9e 100755 --- a/leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/README.md +++ b/leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/README.md @@ -1,28 +1,36 @@ # [2419.Longest Subarray With Maximum Bitwise AND][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 integer array `nums` of size `n`. + +Consider a **non-empty** subarray from `nums` that has the **maximum** possible **bitwise AND**. + +- 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. +Return the length of the longest such subarray. + +The bitwise AND of an array is the bitwise AND of all the numbers in it. + +A **subarray** is a contiguous sequence of elements within an array. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,2,3,3,2,2] +Output: 2 +Explanation: +The maximum possible bitwise AND of a subarray is 3. +The longest subarray with that value is [3,3], so we return 2. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Longest Subarray With Maximum Bitwise AND -```go ``` - +Input: nums = [1,2,3,4] +Output: 1 +Explanation: +The maximum possible bitwise AND of a subarray is 4. +The longest subarray with that value is [4], so we return 1. +``` ## 结语 diff --git a/leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/Solution.go b/leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/Solution.go index d115ccf5e..c23fc7a04 100644 --- a/leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/Solution.go +++ b/leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/Solution.go @@ -1,5 +1,22 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + m := 0 + l := 0 + ans := 0 + for _, n := range nums { + if n == m { + l++ + ans = max(ans, l) + continue + } + if n > m { + m = n + l = 1 + ans = 1 + continue + } + l = 0 + } + return ans } diff --git a/leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/Solution_test.go b/leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/Solution_test.go index 14ff50eb4..07acf9179 100644 --- a/leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/Solution_test.go +++ b/leetcode/2401-2500/2419.Longest-Subarray-With-Maximum-Bitwise-AND/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, 2, 3, 3, 2, 2}, 2}, + {"TestCase2", []int{1, 2, 3, 4}, 1}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }