I'm curious how much you guys care about seeing the most optimal solution? I probably could've gotten this video out a few hours earlier if I just showed the first solution.
I personally watch your videos even after solving the problem because I know you will come up with the most optimal solution. So I want the most optimal solution.
I love optimal solutions, it introduces new and clever way of thinking and in my opinion is what improves problem solving skills and get us out of comfort zone. You are doing great work!
Definitely prefer having the optimal solution, at the end of the day the daily challenges are temporary but the content is useful indefinitely, soI think it's better to take longer but give people the assurance that a solution is optimal or near-optimal
I care more about practicing common patterns that I'll see in interviews than crackhead solutions, which I feel like this one leans towards. Just personal opinion though
For me, I enjoy the way you’re thinking and analyzing for solving these problems. I’m trying to learn how to solve a problem like you. So, no matter it’s optimal solution or not, it’s fine to me. But actually, hope the solution to that question is enough to the interview because I’m preparing for this now.😂
I always love your way of explaining optimal solutions. Even though some of those are so difficult to follow or cannot be applied to other problems, I enjoy watching your intuition or logics in thought process. The only solutions I didn't care so far were solving tree traversal in iterative way, which are more complicated while being neither optimal nor practical
Bro, I had the first step where we try to select the numbers which don't conflict, but the neat part comes into picture when you pick the numbers which do conflict, afterwards its boiling down to house robber and a beautiful way to solve using basic set theory, I am mind blown !!!
Even though I code in Java and usually skip your Python code, I still check out your explanations of how to solve the problems and your thinking process, because you are just damn good at it.
For creating the groups, can we just make the groups according to number's remainder when they are divided by k. So we have k groups initialized for each remainder from 0 to k-1, and we will iterate through all the numbers in array then if num%k == 2 then we will add that num in 2nd index group. Like, group[num%k].push_back(num).
One suggestion, use the statistics formula to simplify the formula. Number of ways in which we can k elements from N elements such that one or more of other elements is not present-> use this as base case
Could there be a mistake in complexity analysis here, cause the similar solution on the editorial section of leetcode claims it's time complexity to be nlogn + 2^n in worst case, which makes sense to me since helper function will be called at most 2 times recursively in each call, and in the worst case all n elements of the array can be in the same group making time complexity of the helper function to be 2^n in the worst case.
Hey @neetcode, I have a curiosity. I have seen your contests graph in Leetcode, no offence this is much impressive but I have noticed some times you have struggled in some contests. My question is how had you overcome those days, like when I can not perform as expected in the contests or can not come up with solutions for the daily challenges, my subconscious mind lets me to rethink if I should continue grinding leetcode or I should quit. Your profile is a true inspiration because I can see myself as struggling as you but sometimes it's difficult to keep myself motivated. I already have a 40hrs/week job and dreaming to crack bigtechs, it's not always easy to be consistently motivated everyday specially when facing some crackhead problems like this and I can not have much time per day for trying hard.
brute force is giving TLE with c++ is it because c++ sucks at recursion or did i do something wrong? class Solution { public: int beautifulSubsets(vector& nums, int k) { int n = nums.size(); function dfs = [&](int i, unordered_map count){ if(i == n) return 1; int res = dfs(i+1, count); if(!count[nums[i]-k] && !count[nums[i]+k]){ count[nums[i]]++; res += dfs(i+1, count); count[nums[i]]--; } return res; }; unordered_map map; return dfs(0, map) - 1; } };
@@NeetCodeIO no, i understood the question well, however, first, with the dfs solution, I wasn’t able to understand, how will that work? By doing a dry run, it helped me understand better. Recommending you to do that. I almost see your vids daily, and My another suggestion is that, there are some newbie people who don’t know meaning of XOR/OR, or the Counter function. It would be worth making separate UA-cam video/short relating to it, and link it in the description of the videos using it. I’m giving my feedback as a high school student, I’m doing leetcode as it’ll be beneficial for my future
import java.util.HashMap; class Solution { public int beautifulSubsets(int[] nums, int k) { return iterator(nums,k,0,new HashMap()); }
public int iterator(int[] nums, int k, int i, HashMap counter){ if(i == nums.length ){ return 1; } int res = 0;
res += iterator(nums,k,i+1,counter); // include if(!counter.containsKey(nums[i]-k) && !counter.containsKey(nums[i]+k)){ counter.put(nums[i],counter.getOrDefault(nums[i],0)+1); res += iterator(nums,k,i+1,counter); counter.put(nums[i],counter.getOrDefault(nums[i],0)-1); } return res; } } What am I doing wrong here?
You're just checking for whether the key exists or not but you should check if the key doesn't exist OR if it does the frequency or count of nums[i] should be zero. Hope it helps. If not I can share my java solution if you want.
I'm curious how much you guys care about seeing the most optimal solution? I probably could've gotten this video out a few hours earlier if I just showed the first solution.
I personally watch your videos even after solving the problem because I know you will come up with the most optimal solution.
So I want the most optimal solution.
I love optimal solutions, it introduces new and clever way of thinking and in my opinion is what improves problem solving skills and get us out of comfort zone. You are doing great work!
Definitely prefer having the optimal solution, at the end of the day the daily challenges are temporary but the content is useful indefinitely, soI think it's better to take longer but give people the assurance that a solution is optimal or near-optimal
I like seeing both simple and optimal solutions!
I care more about practicing common patterns that I'll see in interviews than crackhead solutions, which I feel like this one leans towards. Just personal opinion though
For me, I enjoy the way you’re thinking and analyzing for solving these problems.
I’m trying to learn how to solve a problem like you.
So, no matter it’s optimal solution or not, it’s fine to me.
But actually, hope the solution to that question is enough to the interview because I’m preparing for this now.😂
The optimal solution was crazy. Kudos for explaining it so well.
I always love your way of explaining optimal solutions. Even though some of those are so difficult to follow or cannot be applied to other problems, I enjoy watching your intuition or logics in thought process.
The only solutions I didn't care so far were solving tree traversal in iterative way, which are more complicated while being neither optimal nor practical
As a beginner, I care about a solution. Most importantly understand that solution through a dry run.
Optimization is not my priority as of now.
how did u come up with this solution, like what is your thinking process. pls do make a video about that. And yes i care about the optimal solution.
Bro, I had the first step where we try to select the numbers which don't conflict, but the neat part comes into picture when you pick the numbers which do conflict, afterwards its boiling down to house robber and a beautiful way to solve using basic set theory, I am mind blown !!!
I was able to use the suboptimal approach with just a hashset, as long as I sorted the input array beforehand.
Even though I code in Java and usually skip your Python code, I still check out your explanations of how to solve the problems and your thinking process, because you are just damn good at it.
@NeetCodeIO instead of defaultdict(int), you can use Counter class from collections
For creating the groups, can we just make the groups according to number's remainder when they are divided by k. So we have k groups initialized for each remainder from 0 to k-1, and we will iterate through all the numbers in array then if num%k == 2 then we will add that num in 2nd index group. Like, group[num%k].push_back(num).
One suggestion, use the statistics formula to simplify the formula.
Number of ways in which we can k elements from N elements such that one or more of other elements is not present-> use this as base case
Not sure about the subsets, but your explanation is definitely beautiful!
You are the hero we don't deserve, but we all need
great explanation man i actually understood the optimal solution this time
How do people come up with these solution @NeetCodeIO...
Thank you
First I'm feeling is my preparation is in good way!!😢
Because I solved 20+ problems on backtracking myself but can't understand the this problem
Would an optimal solution be required to pass the interview, or will backtracking be enough?
from 2.6 seconds to 60 milliseconds i would say is pretty huge optimisation. literallty x40
feels good, being able to solve this without neetcode, (for once)! (thanks neet)
```
from collections import defaultdict
class Solution:
def beautifulSubsets(self, nums: List[int], k: int) -> int:
res = []
currPath = []
counter = defaultdict(int)
def dfs(i):
if i >= len(nums):
if currPath:
res.append(currPath.copy())
return
lowerBound = nums[i] - k
upperBound = nums[i] + k
if counter[lowerBound] == 0 and counter[upperBound] == 0:
currPath.append(nums[i])
counter[nums[i]] += 1
dfs(i+1)
currPath.pop()
counter[nums[i]] -= 1
dfs(i+1)
dfs(0)
return len(res)
```
3:20 - Isn't the calculation for the amount of subarrays N*(N+1)/2, not N^2?
yeah i was thinking along the lines of the Big O complexity
@@NeetCodeIO Got it, thanks
This is similar to the question that was asked in codeforces round 945 (I think)
For this question I was stuck in the first answer trying to use a hashset and not knowing why it wasn't working.
thank you 🙂
That's so elegant
bro back after long time
Could there be a mistake in complexity analysis here, cause the similar solution on the editorial section of leetcode claims it's time complexity to be nlogn + 2^n in worst case, which makes sense to me since helper function will be called at most 2 times recursively in each call, and in the worst case all n elements of the array can be in the same group making time complexity of the helper function to be 2^n in the worst case.
Hey @neetcode, I have a curiosity. I have seen your contests graph in Leetcode, no offence this is much impressive but I have noticed some times you have struggled in some contests. My question is how had you overcome those days, like when I can not perform as expected in the contests or can not come up with solutions for the daily challenges, my subconscious mind lets me to rethink if I should continue grinding leetcode or I should quit. Your profile is a true inspiration because I can see myself as struggling as you but sometimes it's difficult to keep myself motivated.
I already have a 40hrs/week job and dreaming to crack bigtechs, it's not always easy to be consistently motivated everyday specially when facing some crackhead problems like this and I can not have much time per day for trying hard.
we need both simple + optimal
insane
I would never come up with the optimal solution in an interview
brute force is giving TLE with c++
is it because c++ sucks at recursion or did i do something wrong?
class Solution {
public:
int beautifulSubsets(vector& nums, int k) {
int n = nums.size();
function dfs = [&](int i, unordered_map count){
if(i == n) return 1;
int res = dfs(i+1, count);
if(!count[nums[i]-k] && !count[nums[i]+k]){
count[nums[i]]++;
res += dfs(i+1, count);
count[nums[i]]--;
}
return res;
};
unordered_map map;
return dfs(0, map) - 1;
}
};
nvm, i passed hash map by reference and it worked
@@moabd7575 yes thats why u backtrack coz you dont have to copy hashmap
What the magic? I don't know how I could find the first solution even
bro try to be consistent
Does anybody expect the optimal solution in an interview?
Fuck optimal solution - The problem with optimal solution is that how many will remember the intuitive when we give interview.
point😆
You're just not explaining "Why are we doing that, how does that help with our problem?", in detail, for some problems. Rest I'm able to understand
Thanks for the feedback. Was there a specific part of the algorithm that you had in mind?
@@NeetCodeIO no, i understood the question well, however, first, with the dfs solution, I wasn’t able to understand, how will that work? By doing a dry run, it helped me understand better. Recommending you to do that.
I almost see your vids daily, and My another suggestion is that, there are some newbie people who don’t know meaning of XOR/OR, or the Counter function. It would be worth making separate UA-cam video/short relating to it, and link it in the description of the videos using it.
I’m giving my feedback as a high school student, I’m doing leetcode as it’ll be beneficial for my future
import java.util.HashMap;
class Solution {
public int beautifulSubsets(int[] nums, int k) {
return iterator(nums,k,0,new HashMap());
}
public int iterator(int[] nums, int k, int i, HashMap counter){
if(i == nums.length ){
return 1;
}
int res = 0;
res += iterator(nums,k,i+1,counter);
// include
if(!counter.containsKey(nums[i]-k) && !counter.containsKey(nums[i]+k)){
counter.put(nums[i],counter.getOrDefault(nums[i],0)+1);
res += iterator(nums,k,i+1,counter);
counter.put(nums[i],counter.getOrDefault(nums[i],0)-1);
}
return res;
}
}
What am I doing wrong here?
You're just checking for whether the key exists or not but you should check if the key doesn't exist OR if it does the frequency or count of nums[i] should be zero.
Hope it helps. If not I can share my java solution if you want.
@@JRK_RIDES It worked. Thank you