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)))
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
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?
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
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 ?
If it's one off problem then don't worry, but if you are finding it hard to comeup with intuition quite frequently after solving 700+ questions means that you are either solving too easy question or similar pattern question. I will recommend you to solve Neetcode 150 questions and make sure you don't watch solution before 1 hour and try to understand solution in case you need to watch solution.
Do you revisit and resolve the problems you've solved before ? If you don't then start doing them now. There's always an element of memorization when you want to understand something but don't overuse it.
@@sachinnair1648 excellent point made. rather than solving 700+ problem its better to solve 350 questions twice so that you can recall pattern much quickly.
Focus on the maximum element. Find the longest contiguous subarray where all elements are equal to this maximum value.
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)))
Surprisingly, Solution of this problem don't require use of bitwise and operator.
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
Did exactly same
@@rambabupatidar3092 good job dude
Thanks a ton, your videos help me a lot. I am trying to polish my coding skills.
Beautifully explained. Thank you !
why is the old channel not active anymore?
great explanation
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?
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?
I was able to solve it in O(n) after reading the first hint.
I think this question is just better phrased as “Find the maximum bitwise value with the longest streak”
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
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 ?
If it's one off problem then don't worry, but if you are finding it hard to comeup with intuition quite frequently after solving 700+ questions means that you are either solving too easy question or similar pattern question. I will recommend you to solve Neetcode 150 questions and make sure you don't watch solution before 1 hour and try to understand solution in case you need to watch solution.
Do you revisit and resolve the problems you've solved before ? If you don't then start doing them now. There's always an element of memorization when you want to understand something but don't overuse it.
@@sachinnair1648 excellent point made. rather than solving 700+ problem its better to solve 350 questions twice so that you can recall pattern much quickly.
Pls do Longest Subarray with sum K | [Postives and Negatives]
i was almost going for a dynamic programming solution until I saw the constraints and noped out
First solution is at least 40ms faster according to leetcode lol. So much for optimization 😂.