Re: [闲聊] 每日leetcode

楼主: devilkool (对猫毛过敏的猫控)   2024-09-14 15:21:58
※ 引述《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;
}

Links booklink

Contact Us: admin [ a t ] ucptt.com