Longest Turbulent Array - Leetcode 978 - Python

  Рет қаралды 13,337

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 23
@khyunwoo1
@khyunwoo1 8 ай бұрын
as someone learning through neetcode, this problem didn't really belong under the kadane's algo section. It had less to do with kadane's algo itself and more to do with sliding window or 2 pointers.
@NeetCodeIO
@NeetCodeIO Жыл бұрын
Today's daily LC is already on the main channel (kzbin.info/www/bejne/Z6HOgJqOeZtmr7c), so uploading another one instead today.
@ary_21
@ary_21 Жыл бұрын
I thought it's gonna be a holiday today , appreciate your dedication
@krakenjoka
@krakenjoka Жыл бұрын
Actually from this explanation, variables tracking max and cur length would be enough. On sliding window problems we usually need the left pointer to check for some conditions. But on this problem it's not that relevant. Thanks for these videos
@alexl2512
@alexl2512 Жыл бұрын
My implementation def maxTurbulenceSize(self, arr: List[int]) -> int: l = 0 maxLen = 0 preS = "=" for r in range(len(arr)): if r == 0: maxLen = 1 continue if arr[r - 1] < arr[r] and preS != "": preS = ">" maxLen = max(maxLen, r - l + 1) elif arr[r - 1] == arr[r]: l = r preS = "=" else: l = r - 1 return maxLen
@rafael84
@rafael84 10 ай бұрын
I think this solution is a bit more easy to read: var maxTurbulenceSize = function (arr) { if (arr.length curr ? 1 : -1; if (newSign === sign) { currCount = 2; } else { currCount += 1; } sign = newSign; } maxCount = Math.max(maxCount, currCount); } return maxCount; };
@trueknowledge1981
@trueknowledge1981 Жыл бұрын
Holy I am the first this time Hello Neetcode
@iamnoob7593
@iamnoob7593 3 ай бұрын
Thanks neetcode
@junjason4114
@junjason4114 11 ай бұрын
My solution to this problem. class Solution: def maxTurbulenceSize(self, arr: List[int]) -> int: if len(arr) == 1: return 1 pre_diff, subarray_len = -(arr[0] - arr[1]), 1 longest = subarray_len for i in range(1, len(arr)): cur_diff = arr[i - 1] - arr[i] if cur_diff * pre_diff < 0: subarray_len, pre_diff = subarray_len + 1, cur_diff continue longest = max(longest, subarray_len) subarray_len = 1 if cur_diff == 0 else 2 pre_diff = cur_diff return max(longest, subarray_len)
@EzraSchroeder
@EzraSchroeder Жыл бұрын
@5:07 math died!!!! XD hahahahahaha!!!!!
@sumitsharma6738
@sumitsharma6738 Жыл бұрын
also for famous problem "OPTIMAL STRATEGY FOR A GAME"
@vivekmit06
@vivekmit06 Жыл бұрын
Thank you very much!!!
@sumitsharma6738
@sumitsharma6738 Жыл бұрын
please make a video for "Super Egg Drop" problem
@Mohit-uq7hq
@Mohit-uq7hq 3 ай бұрын
he sounds like john smilga from free code camp
@menagamal7239
@menagamal7239 Жыл бұрын
Can't we Just Use Stacks for this one ?
@BCS_AAMIRASHRAF
@BCS_AAMIRASHRAF Жыл бұрын
❤❤❤❤❤
@trueknowledge1981
@trueknowledge1981 Жыл бұрын
bdw what is use of left pointer exacltly?
@MeanSoybean
@MeanSoybean Жыл бұрын
to keep track of the beginning of the currently examined candidate for turbulent subarray
@krakenjoka
@krakenjoka Жыл бұрын
I dont think it's necessary, 2 variables, one counting current length and another for max length would be enough
@MeanSoybean
@MeanSoybean Жыл бұрын
@@krakenjoka and how would you compute what the current length is
@krakenjoka
@krakenjoka Жыл бұрын
@@MeanSoybean current length initial value can be 0, then during the for loop, add cur+=1 if it satisfy our condition. Otherwise reset it to 0 or 1 depending on stones[i-1] and stones[i] being equal or not. At every iteration compare global max so far to the current length.
@nehatyagi5218
@nehatyagi5218 Жыл бұрын
hey can you debug my code class Solution { public: int maxTurbulenceSize(vector& arr) { int l =0; int r = 1; int ans =1; int n = arr.size(); string prev = ""; while(r arr[r] && prev != ">"){ ans = max(ans , r-l+1); r += 1; prev = ">"; } else if(arr[r-1] < arr[r] && prev != "
@isseihyodou7111
@isseihyodou7111 Жыл бұрын
The issue with your code lies in how it's handling the case where the array elements are equal (arr[r] == arr[r-1]). The logic doesn't correctly reset the pointers l and r, resulting in incorrect computations for the turbulent subarray length. Here's a modified version of your code that addresses this issue: public: int maxTurbulenceSize(vector& arr) { int l = 0; int r = 1; int ans = 1; int n = arr.size(); string prev = ""; while (r < n) { if (arr[r - 1] > arr[r] && prev != ">") { ans = max(ans, r - l + 1); prev = ">"; } else if (arr[r - 1] < arr[r] && prev != "
Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
8:59
Last Stone Weight II - Leetcode 1049 - Python
19:04
NeetCodeIO
Рет қаралды 16 М.
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 23 МЛН
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 97 МЛН
Maximum Product Subarray - Dynamic Programming - Leetcode 152
15:31
Maximum Subsequence Score - Leetcode 2542 - Python
13:59
NeetCodeIO
Рет қаралды 23 М.
Subarray Sums Divisible by K - Leetcode 974 - Python
16:41
NeetCodeIO
Рет қаралды 18 М.
Valid Parenthesis String - Leetcode 678 - Python
13:43
NeetCode
Рет қаралды 74 М.
Minimize Deviation in Array - Leetcode 1675 - Python
20:40
NeetCodeIO
Рет қаралды 12 М.