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