This commit is contained in:
parent
c896dc5903
commit
510710890d
1 changed files with 17 additions and 0 deletions
17
medium/longest_nice_subarray.py
Normal file
17
medium/longest_nice_subarray.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# https://leetcode.com/problems/longest-nice-subarray
|
||||
|
||||
from typing import List
|
||||
|
||||
class Solution:
|
||||
def longestNiceSubarray(self, nums: List[int]) -> int:
|
||||
left = 0
|
||||
or_mask = 0
|
||||
result = 0
|
||||
for right in range(len(nums)):
|
||||
while (or_mask & nums[right]) != 0:
|
||||
or_mask ^= nums[left]
|
||||
left += 1
|
||||
or_mask |= nums[right]
|
||||
result = max(result, right - left + 1)
|
||||
return result
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue