※ 引述《dont (dont)》之铭言:
: 2419. Longest Subarray With Maximum Bitwise AND
看了hint才知道AND的习性
复习个sliding window O(n)
C#:
public int LongestSubarray(int[] nums)
{
int maxLength = 0, max = 0, left = 0, right = 0;
while (right < nums.Length)
{
if (max < nums[right])
{
max = nums[right];
maxLength = 1;
left = right;
}
else if (max == nums[right])
{
maxLength = Math.Max(maxLength, right - left + 1);
}
else
{
left = right + 1;
}
right++;
}
return maxLength;
}