leetpycode/medium/longest_nice_subarray.py

17 lines
471 B
Python

# 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