Longest Continuous Subarray with Absolute Diff Less than or Equal to Limit - Leetcode 1438 - Python

  Рет қаралды 10,393

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 32
@akash-kumar737
@akash-kumar737 5 ай бұрын
Slinding Window Max is probably very easy in comparison to this. Important lesson form such problem is that don't waste more than 30 minutes on one problem and quickly look for solution 😅.
@tirasjeffrey2002
@tirasjeffrey2002 5 ай бұрын
today i found out theres something called monotonic queues (increasing and decreasing queues),,, you know a question is tough as fu** when you're learning a new data structure 💀💀💀
@rahulsharma-hu7vr
@rahulsharma-hu7vr 5 ай бұрын
But remember when you hear monotonic, it means it is going in only one direction, like only increasing or only decreasing. If both are possible in single array of something, it is not monotonic anymore.
@kthtei
@kthtei 5 ай бұрын
got to point where I had to use sliding window with min, max value captured. I also tried using a deque before that, just couldn't sum these ideas together, it was close though. Pretty complicated one, wouldn't say hard but mid-hard ish.
@amol_
@amol_ 4 ай бұрын
I think using TreeSet it is easy (TreeSet implementation of red black tree) class Solution { public int longestSubarray(int[] nums, int limit) { TreeSet set = new TreeSet((a, b) -> nums[a] == nums[b] ? a - b : nums[a] - nums[b]); int left = 0; int res = 1; set.add(0); for(int right = 1; right < nums.length; right++) { set.add(right); while(nums[set.last()] - nums[set.first()] > limit) { set.remove(left++); } res = Math.max(res, right - left + 1); } return res; } }
@chaitanyasharma6270
@chaitanyasharma6270 5 ай бұрын
After yesterdays question i thought i could be smart and just use 4 pointers for the second smallest and the second largest elements as well. But i am not smart enough to implement it just used two heaps instead
@chaitanyasharma6270
@chaitanyasharma6270 5 ай бұрын
But the thought process of the second smallest and the second largest kinda led me to the two heaps solution
@chaitanyasharma6270
@chaitanyasharma6270 5 ай бұрын
Like duh why am I thinking so much about how to move the pointers to get the next smallest and the next largest. When i can just delegate that to a heap
@kolhesatish
@kolhesatish 5 ай бұрын
​@@chaitanyasharma6270you are thinking right
@zz-yy-xx
@zz-yy-xx 5 ай бұрын
You explanation is always the best. Always wait for your video!!!
@nirmalgurjar8181
@nirmalgurjar8181 5 ай бұрын
Solved it using 2 pqs, but came to see dq version. nlogn to n .. very well explained.
@muffincodingchannel
@muffincodingchannel 5 ай бұрын
I'm inexperienced with monotonic queues and used a multiset/heap - passed, but was very slow.
@bhavyajainnd
@bhavyajainnd 5 ай бұрын
I was able to solve it using a TreeMap. Basically I keep inserting (nums[right], count) to map. So min element is always map.first and max element is always map.last. The rest of the logic is the same. I did get a much slower time though but it passed unfortunately.
@pdjeowudjx
@pdjeowudjx 5 ай бұрын
your voice changed!
@rahulnegi456
@rahulnegi456 5 ай бұрын
yeah i noticed this too, maybe he bought a new mic
@satyamjha68
@satyamjha68 5 ай бұрын
Solved it!!
@pastori2672
@pastori2672 5 ай бұрын
no shot
@oii0712
@oii0712 5 ай бұрын
i found a brute force and it worked for smaller number but failed at number with big arays
@SubhamKumar-eg1pw
@SubhamKumar-eg1pw 5 ай бұрын
Chain of Thought :skull:
@thunderstorm-d2c
@thunderstorm-d2c 5 ай бұрын
Hi neetcode, Really appreciate your excellent video! I have a question about the editorial in leetocde. In their python solution 2, SortedDict from sortedcontainers is used. However, sortedcontainers is not a bulid in package in python. So, are we allowed to used it in common OA platforms(eg codesignal)? I tried it in a random question in hackerrank and had an import error. I also tried to look it up online, but did not find an exact answer.
@NeetCodeIO
@NeetCodeIO 5 ай бұрын
yeah it's not available in many platforms (fyi it should be available on neetcode io) usually people use it in place of treemaps, since python doesnt have that. in most interviews i think it would be acceptable to use it, since languages like Java and C++ have treemaps built in. that said, usually problems involving tree maps can also be solved with heaps, so i prefer the heap solutions since at least i know heaps are always available in python
@thunderstorm-d2c
@thunderstorm-d2c 5 ай бұрын
@@NeetCodeIO Thanks for your response, especially the part about using heap!
@akashverma5756
@akashverma5756 5 ай бұрын
using multiset is alot easier but time is nlogn
@meemee417
@meemee417 4 ай бұрын
queues gotta be the most unintuitive thing ever
@dev9844
@dev9844 5 ай бұрын
thanks for showing the secret jutsu
@harikrishnasadhu9581
@harikrishnasadhu9581 5 ай бұрын
Finally!!
@mihirkotecha9963
@mihirkotecha9963 5 ай бұрын
why so late today??
@EduarteBDO
@EduarteBDO 5 ай бұрын
I think this question is medium because you can solve it in O^2, with a little trick: impl Solution { pub fn longest_subarray(nums: Vec, limit: i32) -> i32 { let limit = limit as u32; let mut res = 0; for l in 0..nums.len() { let mut min_val = i32::MAX; let mut max_val = i32::MIN; for r in l..nums.len() { min_val = min_val.min(nums[r]); max_val = max_val.max(nums[r]); if min_val.abs_diff(max_val) > limit { break; } res = res.max((r - l) as i32 + 1); } if res >= (nums.len() - l) as i32 { break; } } res } }
@grantpeterson2524
@grantpeterson2524 5 ай бұрын
Another Rust user 🫡 yeah I've noticed a lot of time that Rust solutions pass anyways even if they aren't optimal just because it runs too fast to quantify (lots of problems just run in 0ms), but it's more important to learn the correct algorithms even if you can technically cheat the system.
@chien-yuyeh9386
@chien-yuyeh9386 5 ай бұрын
🎉🎉🎉
@CuriousAnonDev
@CuriousAnonDev 5 ай бұрын
If someone is not that new to leetcode and dsa, this question is not that hard Once you realise you need max and min You try heap, then you realise there might be elements from out of the window in the heap, so you think of queue
@chandlerbing8164
@chandlerbing8164 5 ай бұрын
Late This time.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 151 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 695 М.
Кто круче, как думаешь?
00:44
МЯТНАЯ ФАНТА
Рет қаралды 6 МЛН
Чистка воды совком от денег
00:32
FD Vasya
Рет қаралды 2,7 МЛН
Subarrays with K Different Integers - Leetcode 992 - Python
17:31
10 Crazy Python Operators That I Rarely Use
11:37
Indently
Рет қаралды 43 М.
Minimum Cost to Hire K Workers - Leetcode 857 - Python
19:01
NeetCodeIO
Рет қаралды 15 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 303 М.
Evaluate Division - Leetcode 399 - Python
17:37
NeetCodeIO
Рет қаралды 33 М.
Кто круче, как думаешь?
00:44
МЯТНАЯ ФАНТА
Рет қаралды 6 МЛН