Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Sep 14, 2024
1 parent 18b4036 commit 9530df3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
40 changes: 40 additions & 0 deletions leetcode/2419.LongestSubarrayWithMaximumBitwiseAND/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/description/

package main

import (
"fmt"
)

func longestSubarray(nums []int) int {
// Step 1: Find the maximum value in the array
maxVal := nums[0]
for _, num := range nums {
if num > maxVal {
maxVal = num
}
}

// Step 2: Find the length of the longest subarray where all elements equal maxVal
longest := 0
currentLength := 0

for _, num := range nums {
if num == maxVal {
currentLength++
if currentLength > longest {
longest = currentLength
}
} else {
currentLength = 0
}
}

return longest
}

func main() {
nums := []int{1, 2, 3, 3, 2, 2, 3}

fmt.Println(longestSubarray(nums))
}
20 changes: 20 additions & 0 deletions leetcode/2419.LongestSubarrayWithMaximumBitwiseAND/sol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def longestSubarray(nums):
max_val = max(nums)

longest = 0
current_length = 0

for num in nums:
if num == max_val:
current_length += 1
longest = max(longest, current_length)
else:
current_length = 0

return longest

if __name__ == "__main__":
nums = [1, 2, 3, 3, 2, 2, 3]

result = longestSubarray(nums)
print(result)

0 comments on commit 9530df3

Please sign in to comment.