Re: [闲聊] 每日leetcode

楼主: dont   2024-09-14 14:24:49
2419. Longest Subarray With Maximum Bitwise AND
## 思路
AND值会变小, 所以目标是找里面都是相同最大值的subarray
用while循环感觉好长 唉
## Code
```python
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
res = curr_max = 0
i, n = 0, len(nums)
while i < n:
cnt = 1
while i + cnt < n and nums[i+cnt] == nums[i]:
cnt += 1
if nums[i] > curr_max:
curr_max, res = nums[i], cnt
elif nums[i] == curr_max:
res = max(res, cnt)
i += cnt
return res
```
作者: DJYOMIYAHINA (通通打死)   2024-09-14 14:38:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com