Longest Subarray With Maximum Bitwise AND - Leetcode 2419 - Python

preview_player
Показать описание


0:00 - Read the problem
1:50 - Drawing Explanation
10:12 - Two Pass Coding
10:58 - One Pass Coding

leetcode 2419

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

Focus on the maximum element. Find the longest contiguous subarray where all elements are equal to this maximum value.

ankitdimri
Автор

Thanks for your video!
Here are a couple of oneliners:

def longestSubarray(self, a):
return max((v, len([*p])) for v, p in groupby(a))[1]

def longestSubarray(self, a):
return max(accumulate([0]+a, lambda l, v, M=max(a):(l+1)*(v==M)))

MikPosp
Автор

Surprisingly, Solution of this problem don't require use of bitwise and operator.

akashverma
Автор

Hi Navdeep. Solved it on my own its a tricky leetcode question.
My code below --

class Solution:
def longestSubarray(self, nums: List[int]) -> int:
N = len(nums)
maxVal = max(nums)
i = 0
maxLen = 0

while i < N:
num = nums[i]
if num == maxVal:
count = 0
while i < N and nums[i] == maxVal:
count += 1
i += 1
maxLen = max(maxLen, count)
i+= 1

return maxLen

freecourseplatformenglish
Автор

why is the old channel not active anymore?

orepajic
Автор

Thanks a ton, your videos help me a lot. I am trying to polish my coding skills.

cherryayethihan
Автор

All you need to do is to find the largest number.. and then, find the length of the longest subarray that has all values as the largest number !

As, once you have the largest number, that is the maximum And value that can be achieved in that array and, the And maximum value can only stay maximum if it's anded to itself, hence you need to find the length of the longest subarray that has all values equal to the max value

Code Below :

def longestSubarray(self, nums: List[int]) -> int:
max_ct=0
ct = 0
maxx = max(nums)

for n in nums:
if n == maxx:
ct+=1
max_ct=max(ct, max_ct)
else:
ct=0

return max_ct

vnnyboy
Автор

I was able to solve it in O(n) after reading the first hint.

ShinAkuma
Автор

I think this question is just better phrased as “Find the maximum bitwise value with the longest streak”

henokassalif
Автор

At 8:29, you say it is just finding the longest subarray with the maximum element, but what if the subarray was 3, 3, 3, 7, 3? The 3 ANDed with the 7 still produces a 3 so you wouldn't want to restart your window when you see the 7, right?

nethpot
Автор

why i cant visualize things like this on my own, like ive solved like in 700+problem but still 1720 rated never solved hard question in contest is there a way i can improve myself like topics on which i have to focus to improve myself ?

phantomhawk
Автор

How about NEGATIVE numbers?
-1 is all 1s in binary, so (-1) & n == n for every n. Do we need to use a proper sliding window then?

dmitrykarpenko
Автор

i was almost going for a dynamic programming solution until I saw the constraints and noped out

_PranavDesai
Автор

Pls do Longest Subarray with sum K | [Postives and Negatives]

Spongebob-gtrg
Автор

First solution is at least 40ms faster according to leetcode lol. So much for optimization 😂.

kartikeyaarun