Longest Turbulent Array - Leetcode 978 - Python

  Рет қаралды 14,097

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 23
@khyunwoo1
@khyunwoo1 9 ай бұрын
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 Жыл бұрын
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; };
@sumitsharma6738
@sumitsharma6738 Жыл бұрын
also for famous problem "OPTIMAL STRATEGY FOR A GAME"
@vivekmit06
@vivekmit06 Жыл бұрын
Thank you very much!!!
@iamnoob7593
@iamnoob7593 4 ай бұрын
Thanks neetcode
@trueknowledge1981
@trueknowledge1981 Жыл бұрын
Holy I am the first this time Hello Neetcode
@sumitsharma6738
@sumitsharma6738 Жыл бұрын
please make a video for "Super Egg Drop" problem
@EzraSchroeder
@EzraSchroeder Жыл бұрын
@5:07 math died!!!! XD hahahahahaha!!!!!
@junjason4114
@junjason4114 Жыл бұрын
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)
@Mohit-uq7hq
@Mohit-uq7hq 4 ай бұрын
he sounds like john smilga from free code camp
@menagamal7239
@menagamal7239 Жыл бұрын
Can't we Just Use Stacks for this one ?
@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.
@BCS_AAMIRASHRAF
@BCS_AAMIRASHRAF Жыл бұрын
❤❤❤❤❤
@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 != "
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
She made herself an ear of corn from his marmalade candies🌽🌽🌽
00:38
Valja & Maxim Family
Рет қаралды 18 МЛН
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 191 М.
Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
8:59
Majority Element - Leetcode 169 - Python
14:39
NeetCode
Рет қаралды 116 М.
Object-Oriented Programming is Embarrassing: 4 Short Examples
28:03
Brian Will
Рет қаралды 2,1 МЛН
How I Mastered Data Structures and Algorithms in 8 Weeks
15:46
Aman Manazir
Рет қаралды 151 М.
How Senior Programmers ACTUALLY Write Code
13:37
Thriving Technologist
Рет қаралды 1,6 МЛН
Arranging Coins - Leetcode 441 - Python
12:53
NeetCode
Рет қаралды 39 М.
Use Arc Instead of Vec
15:21
Logan Smith
Рет қаралды 158 М.