I'm Japanese but, I didn't expect to hear Japanese on this channel.
@anonanon659611 сағат бұрын
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%.
@ScyneoPL4 сағат бұрын
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
@anonanon130516 сағат бұрын
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)
@harshatc569714 сағат бұрын
Even I solved it by using hash set
@oldmajor524014 сағат бұрын
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
@akhildesaii14 сағат бұрын
HashSet actually does work here! You would just keep on closing your window until you can add the element to your window
@Corgislife121522 сағат бұрын
First to comment, last to solve the question
@yhbarve22 сағат бұрын
Second to comment, last to solve
@zxcvblee647622 сағат бұрын
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.
@icvetz20 сағат бұрын
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.
@devmahad8 сағат бұрын
thanks :)
@ngneerin8 сағат бұрын
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
@ajaygupta60344 сағат бұрын
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
@satyamchauhan977514 сағат бұрын
We can do it with a hashset too bro
@bhanunani13078 сағат бұрын
please make video on 493.Reverse Pairs
@WhyHighC20 сағат бұрын
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?
@ashutoshagrawal787119 сағат бұрын
The techniques are fundamental to problem solving in computer science.
@moralized18 сағат бұрын
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.
@dp8jl19 сағат бұрын
Can y’all solve questions this fast in interview and also optimize it?
@nagendravinayravisetti-jc6yv21 сағат бұрын
hash map justu .... only true fan of naruto can do that 🤣🤣🤣🤣🤣