Leetcode 90. Subsets II

preview_player
Показать описание
Leetcode 90. Subsets II

My contact details

You can practice for coding interview from here

I am Mohammad Fraz , a final year Engineer at DTU and I create content for Coding and technical interviews for FAANG. I explain the intuition to solve Data Structure and Algorithm Questions from leetcode and other platforms. I also cover interview experiences for FAANG and other tech giants. You will also find lectures on the frequently asked interview questions.
Рекомендации по теме
Комментарии
Автор

I think it was simpler to add this line mentioned below after the pop statement in subset 1 problem's code as this works just fine and we don't need the flag part:
while(i < nums.size()-1 && nums[i] == nums[i+1]) i++;

AbhishekYadav-zxpj
Автор

Thanks a lot fraz for this wonderful explanation... really grateful to have u as my mentor..

shraban
Автор

it is actually 2^n * n since if we draw a recursion tree, to reach every leaf node, we need to cover n edges( that is n recursion calls). And Space complexity is O(n) since the max depth of the recursion tree is n. In space complexity part, I have considered the extra space complexity apart from the output vector and input. If we factor in output vector, then definitely, it will be exponential.

dheerjain
Автор

Hi Fraz Sir,
We can just check if last element of temp == nums[i], then don't call the right side recursion
In Java as below
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> answer = new ArrayList();
List<Integer> temp = new ArrayList();
helper(nums, 0, temp, answer);
return answer;
}
public void helper(int [] nums, int i, List<Integer> temp, List<List<Integer>> answer)
{
if(i==nums.length)
{
answer.add(new ArrayList(temp));
}
else
{
int flag=0;
if(temp.size()-1>=0 &&
{
flag=1;
}
else
{
flag=0;
}

temp.add(nums[i]);
helper(nums, i+1, temp, answer);
temp.remove(temp.size()-1);
if(flag==0)
helper(nums, i+1, temp, answer);
}
}
}

umangrajendrabarbhayamcs
Автор

like he said we should ignore all the exes😏😏

viratkohli
Автор

Sir I have a question that when u see this question 1st time u solve correctly or not without seeing solutions .

DeepakKumar-xyrw
Автор

can you please explain with [5, 5, 5, 5, 5] this test case by making a recursive tree.

AnmolKumar-ocwu
Автор

hey can you tell me how you are able to paste code in leetcode? I tried but Ctrl+v won't work and so ctrl+shift+v.

shivcharansinghrawat
visit shbcf.ru