for(int i = 0; i < nums.length; i++){ if(used[i] || i > 0 && nums[i] == nums[i-1] && !used[i - 1]) continue; used[i] = true; tempList.add(nums[i]); backtrack(list, tempList, nums, used); used[i] = false; tempList.remove(tempList.size() - 1); } This will be better optimization
@TON-108
3 ай бұрын
Right! If we find again in base case, it will add extra head of O(n). As our array is already sorted its better to use your mentioned approach 💯
@ImranKhan-ne3cj
Жыл бұрын
great man appreciate your work !
@lalithasrich2170
11 ай бұрын
in recursive call where you are passing nums not the { I,L,Y} how it works
@user-oc7dv7xn9l
3 ай бұрын
U are amazing! However, could u explain why did we need to sort array?
@nikoo28
3 ай бұрын
So that you are covering all similar cases. You can have two digits that are same, if they are all grouped together, they can be removed as duplicates
@jayendraawasthi2646
Жыл бұрын
You are anyway checking for the condition that if the obtained list is already contained in the results so it'll work without the used array condition too right?....i understand some function calls will be prevented with it's use but it'll still work without it right?
@molikaagarwal1135
2 жыл бұрын
Awsm
@user-hb5xr8sj1u
7 ай бұрын
thanku sir
@johncho9160
Жыл бұрын
if we have an int[] = {1,1,2,5} as an ex, the first iteration for the for loop within the backtrack method it will skip the first if statement since used[i] will be false. then we set the used[i] = true then add the element to tempList. then we backtrack then set used[i] = false then remove the element from the tempList. my question is for the second iteration for the loop, used[i] will still be false but our intention is to simply skip right? how and when will it ever hit the first if statement for when used[i] = true?
@johncho9160
Жыл бұрын
btw i came here from watching your video on permutations 1 and i love your explanations. you got yourself a subscriber🔥
@johncho9160
Жыл бұрын
if its not much trouble, do you mind answering my above question?
@nikoo28
Жыл бұрын
i think I am not understanding your question correctly... can you explain with a simpler example? I can try to help.
@deeppanchal5102
Ай бұрын
There is some issue with ur mic or voice
@dishantkumawat7508
9 ай бұрын
if we are already checking if the resultList contains tempList of not then why to use this 'used' array. 😒
@nikoo28
9 ай бұрын
can you please clarify your question with an example? I am unable to follow
@mohamedhamshakothars4238
5 күн бұрын
We can use sending the start index as parameter to the recursive call like you did in permutation 1 video instead of using a used array why using this array which has more space complexity
Пікірлер: 26