Maximum Sum of Distinct Subarrays With Length K - Leetcode 2461 - Python

  Рет қаралды 4,946

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер
@nirakka2222
@nirakka2222 13 сағат бұрын
I'm Japanese but, I didn't expect to hear Japanese on this channel.
@anonanon6596
@anonanon6596 11 сағат бұрын
Using an array was as usual faster than using a hash map. In this exercise it was 7 times faster for me, with memory higher by only 10%.
@ScyneoPL
@ScyneoPL 4 сағат бұрын
Exactly, much faster to use more memory (won't be too bad since it will be 10^5 elements max), than spend time computing hashes two times per iteration and resizing hashmap
@anonanon1305
@anonanon1305 16 сағат бұрын
I think the HashSet is enough to solve this problem, but you need to play with the length of the HashSet class Solution: def maximumSubarraySum(self, nums: List[int], k: int) -> int: if k==1: return max(nums) l=0 count=0 ans=0 checker=set() for r in range(len(nums)): if nums[r-1]==nums[r] and r!=0: l=r count=nums[l] checker=set() checker.add(nums[l]) else: count+=nums[r] checker.add(nums[r]) if len(checker)
@harshatc5697
@harshatc5697 14 сағат бұрын
Even I solved it by using hash set
@oldmajor5240
@oldmajor5240 14 сағат бұрын
here is a more eloquent code incorporating the hashset idea (based on neetcode's implementation): class Solution: def maximumSubarraySum(self, nums: List[int], k: int) -> int: res = 0 window = set() cur_sum = 0 l = 0 for r in range(len(nums)): cur_sum += nums[r] while nums[r] in window or r - l + 1 > k: window.remove(nums[l]) cur_sum -= nums[l] l += 1 if r - l + 1 == k: res = max(res, cur_sum) window.add(nums[r]) return res
@akhildesaii
@akhildesaii 14 сағат бұрын
HashSet actually does work here! You would just keep on closing your window until you can add the element to your window
@Corgislife1215
@Corgislife1215 22 сағат бұрын
First to comment, last to solve the question
@yhbarve
@yhbarve 22 сағат бұрын
Second to comment, last to solve
@zxcvblee6476
@zxcvblee6476 22 сағат бұрын
Appreciate you gave two different methods, but just wonder in what way would second solution be more optimal ? I thought no matter TC or MC are same.
@icvetz
@icvetz 20 сағат бұрын
My assumption would be that .pop() may be expensive on dictionaries. I'm more of a C++ programmer, but avoiding frequent allocations/deallocations is usually preferable. Usually for LC TC is most of what matters, but I've had solutions for certain problems where I had a better runtime despite doing operations with worse time complexities because of other factors.
@devmahad
@devmahad 8 сағат бұрын
thanks :)
@ngneerin
@ngneerin 8 сағат бұрын
Any issue with this solution? s = set() i = 0 j = 0 while j < len(nums): if len(s) < k: if nums[j] in s: i = j s = set() else: s.add(nums[j]) c += nums[j] else: ans = max(ans, c) c -= nums[i] s.remove(nums[i]) return ans
@ajaygupta6034
@ajaygupta6034 4 сағат бұрын
this is the complete code, using your approach: class Solution: def maximumSubarraySum(self, nums: List[int], k: int) -> int: s=set() left=0 right=0 sum=0 res=0 while rightk: sum-=nums[left] s.remove(nums[left]) left+=1 if len(s)==k: res=max(sum,res) right+=1 return res
@satyamchauhan9775
@satyamchauhan9775 14 сағат бұрын
We can do it with a hashset too bro
@bhanunani1307
@bhanunani1307 8 сағат бұрын
please make video on 493.Reverse Pairs
@WhyHighC
@WhyHighC 20 сағат бұрын
Idk how I found this channel, but I am new to coding… when would this need to be applied? Or is this just challenges for the sake of challenges?
@ashutoshagrawal7871
@ashutoshagrawal7871 19 сағат бұрын
The techniques are fundamental to problem solving in computer science.
@moralized
@moralized 18 сағат бұрын
LeetCode problems challenge your data structures/algorithms knowledge, which many people say is a skill set that isn't really useful for their software engineering jobs. However, many companies use these LeetCode style questions in a technical interview to select their candidates, so it's important to know how to solve these types of problems for an interview.
@dp8jl
@dp8jl 19 сағат бұрын
Can y’all solve questions this fast in interview and also optimize it?
@nagendravinayravisetti-jc6yv
@nagendravinayravisetti-jc6yv 21 сағат бұрын
hash map justu .... only true fan of naruto can do that 🤣🤣🤣🤣🤣
@Axel.Blazer
@Axel.Blazer 17 сағат бұрын
this world shall know hash map!
Defuse the Bomb - Leetcode 1652 - Python
13:57
NeetCodeIO
Рет қаралды 7 М.
C#'s Best features you might not be using
31:20
dotnet
Рет қаралды 19 М.
Мама у нас строгая
00:20
VAVAN
Рет қаралды 9 МЛН
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 10 МЛН
How Much Tape To Stop A Lamborghini?
00:15
MrBeast
Рет қаралды 201 МЛН
МЕНЯ УКУСИЛ ПАУК #shorts
00:23
Паша Осадчий
Рет қаралды 4,6 МЛН
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 634 М.
Shortest Subarray with Sum at Least K - Leetcode 862 - Python
27:57
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 297 М.
The Strange Physics Principle That Shapes Reality
32:44
Veritasium
Рет қаралды 6 МЛН
Object-Oriented Programming is Embarrassing: 4 Short Examples
28:03
Brian Will
Рет қаралды 2,1 МЛН
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 235 М.
Intro to Java Programming - Course for Absolute Beginners
3:48:25
freeCodeCamp.org
Рет қаралды 3,5 МЛН
Мама у нас строгая
00:20
VAVAN
Рет қаралды 9 МЛН