Subsets II - Backtracking - Leetcode 90 - Python

Поділитися
Вставка
  • Опубліковано 7 січ 2025

КОМЕНТАРІ • 126

  • @Jr-xs9hy
    @Jr-xs9hy 3 роки тому +198

    "You might be thinking, dynamic programming." - Nope not at all I cant even dynamically program

  • @aleksanderdukaczewski2051
    @aleksanderdukaczewski2051 3 роки тому +138

    Thanks for all the videos, I just got an Amazon offer today and these helped me a lot : )

  • @sumosam1980
    @sumosam1980 3 роки тому +41

    Great solution. This is a better solution than the official Leetcode solution as it honors the spirt of keep/not keep an element that is a hallmark for subset problems.

  • @The6thProgrammer
    @The6thProgrammer Рік тому +1

    Love the video as always. For those curious how to calculate the actual number of subsets. Because repeated values are allowed the technical number of subsets is the multiplicity of every element + 1, multiplied together. For instance, for [1, 2, 2], we see 1 appears once and 2 appears twice. So (1 + 1) + (2 + 1) = 2 * 3 = 6. That is the number of subsets. This works because we are determining how many times we can use each value and multiplying those numbers together (e.g. we can use 2, zero, one, or two times, which is 3 choices). I'd imagine 2^n is an appropriate approximation.

  • @VikramSingh-qu9in
    @VikramSingh-qu9in 2 роки тому +12

    The clarity of thought in your explanations is note worthy :)

  • @andreytamelo1183
    @andreytamelo1183 2 роки тому +6

    Thanks!

  • @ayoubalem865
    @ayoubalem865 3 роки тому +3

    I used the same approach you have used for the combinations sum II problem and it worked for this problem too !
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
    nums.sort()
    res = list()
    numsLen = len(nums)
    def back(cur, pos):
    res.append(cur.copy())
    prev = -11
    for i in range(pos, numsLen):
    if prev == nums[i]:
    continue
    cur.append(nums[i])
    back(cur, i + 1)
    cur.pop()
    prev = nums[i]
    back(list(), 0)
    return res

  • @livinggood1747
    @livinggood1747 2 роки тому +10

    Kept the same decision tree as the first subset problem, and I used a hashmap instead to keep track of the duplicates with a frequency.. so at each leaf node the subset should have a frequency of 1. Otherwise we do append it.

    • @fadsa342
      @fadsa342 2 роки тому

      Doing it with a hashmap is 2^n instead of n*2^n right?

    • @romo119
      @romo119 Рік тому +1

      @@fadsa342 the n portion is from copying an array to the return list, it's unavoidable

    • @romo119
      @romo119 Рік тому +2

      You're still calculating the duplicates then so it doesn't improve runtime. Solution he provides eliminates duplicates as an option entirely

    • @esiebomaj
      @esiebomaj Рік тому +1

      would love to see this your solution

    • @stouser1296
      @stouser1296 2 місяці тому

      @@esiebomaj
      class Solution:
      def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
      subsets = {}
      nums.sort()
      current = []
      def backtrack(i):
      if i >= len(nums):
      subsets[tuple(current)] = True
      return
      current.append(nums[i])
      backtrack(i+1)
      current.pop()
      backtrack(i+1)

      backtrack(0)
      return subsets.keys()

  • @madtoiletpaper
    @madtoiletpaper 3 роки тому +1

    Always excited when I see you post!

  • @haibangi
    @haibangi Рік тому +4

    Thanks for the video. So basically Subsets I but with a sort and shifting i till it's a different number.

  • @gyan2664
    @gyan2664 2 роки тому +1

    Another workable solution with decision tree
    class Solution(object):
    def solve(self, nums, n, temp=[]):

    if n == 0:
    if temp not in self.result:
    self.result.append(temp)
    return

    self.solve(nums, n-1, temp+[nums[n-1]])
    self.solve(nums, n-1, temp)

    def subsetsWithDup(self, nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    self.result = []
    self.solve(sorted(nums), len(nums))
    return self.result

  • @shubhamk9019
    @shubhamk9019 Рік тому +5

    I don't understand why the TC is O(N*2^N). Is it because we can have almost N elements equal to each other?

    • @prathishgm1493
      @prathishgm1493 Рік тому

      check for 'n' (input) how many calls we are making.

  • @mounishgoud8417
    @mounishgoud8417 2 роки тому +1

    Buddy you are awesome. Love from India❤

  • @kelanl7391
    @kelanl7391 Рік тому +1

    played around with what i remembered from the original subsets question:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
    subsets, subset = [], []
    def dfs(i):
    if i >= len(nums):
    subsets.append(subset.copy()) # add subset to res
    return # base case
    subset.append(nums[i]) # add curr to subset
    dfs(i+1) # recurse to go next
    n = subset.pop()
    if n in subset: return # avoid a duplicate subset
    dfs(i+1)
    dfs(0)
    return subsets

  • @vadimkokielov2173
    @vadimkokielov2173 2 роки тому

    something i discovered, similar to this solution:
    if you build a hashmap / histogram of the input, you can then cascade down the frequencies
    for example, let's say
    1: 1
    2: 2
    Then you can do
    1: 1
    2: 2
    [1,2,2]
    1: 1
    2:1
    [1,2]
    1:1
    2:0
    [1]
    1:0
    2:2
    [2,2]
    1:0
    2:1
    [2]
    1:0
    2:0
    []
    Just turn these into subsets by emitting as many of each number as you see in the histogram at the leaves of the decision tree -- and you get all the subsets

    • @markolainovic
      @markolainovic 2 роки тому

      Hey, got a code snippet for this? Can't figure out how to go about it.

    • @vadimkokielov2173
      @vadimkokielov2173 2 роки тому +2

      @@markolainovic yes, i’ll open up leetcode and find my solution for you later today :)

    • @markolainovic
      @markolainovic 2 роки тому

      @@vadimkokielov2173 Thanks! ^.^

    • @samuraijosh1595
      @samuraijosh1595 Рік тому

      @@vadimkokielov2173 wheres the code, you liar.

  • @bluejimmy168
    @bluejimmy168 2 роки тому +1

    at 1:35, [1,2] and [1,2] can not be in the solution, but [2,2] and [1,2,2] can be? Both [2,2] and [1,2,2] have duplicates. Why are they acceptable in the solution? [2,2] and [1,2,2] are not sets? A power set is the set of all subsets. A subset is a set itself. They violate the set principle? Thanks.

    • @PhucNguyen-or6ew
      @PhucNguyen-or6ew 2 роки тому

      [1,2] and [1,2] are identical, [2,2] and [1,2,2] are not, the first one does not include 1 so two of them are different, the response should include unique subset in the list
      the data type used is list, not set, list can have multiple occurrences, set cannot
      hope this help

  • @AndrijaMilikic
    @AndrijaMilikic 7 місяців тому

    This guy is a gem

  • @VarunMittal-viralmutant
    @VarunMittal-viralmutant 2 роки тому +1

    This solution works just fine, nothing fancy required with adjusting 'i' :
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
    combinations = set()
    nums.sort()
    def dfs(i, curr_elem):
    if i >= len(nums):
    combinations.add(tuple(curr_elem))
    return
    elems = curr_elem[:]
    elems.append(nums[i])
    dfs(i+1, elems)
    elems.pop()
    dfs(i+1, elems)
    dfs(0, [])
    return combinations

    • @VarunMittal-viralmutant
      @VarunMittal-viralmutant 2 роки тому +1

      **Note**: Though the above posted code works, it tries to add duplicates to the combinations which set() takes care of eliminating. But the fact that duplicates are being added means that code is taking duplicate branches, which it should not.
      Actually this question is exactly same as the 'Combination Sum ua-cam.com/video/rSA3t6BDDwg/v-deo.html' with just 1 twist, explained in code comment below:
      def print_combinations(nums):
      nums.sort()
      combos = []
      l_nums = len(nums)
      def backtrack(i, curr_elems):
      if i == l_nums:
      combos.append(tuple(curr_elems))
      return

      prev = None
      elems = curr_elems[:]
      for index, num in enumerate(nums[i:], start=i):
      if num == prev:
      continue
      prev = num
      elems.append(num)
      backtrack(index+1, elems)
      elems.pop()
      """
      This extra line here is very important to handle all cases
      otherwise the for loop will always add something to the curr_elem
      The case where we don't add anything and just increment index is handled here
      Plus we are doing it out of loop to avoid duplicates
      """
      backtrack(index+1, elems)
      backtrack(0, [])
      return combos

    • @halahmilksheikh
      @halahmilksheikh 2 роки тому +2

      @@VarunMittal-viralmutant Hi Varun, regarding your first solution with the Set, in order to not have to use the set, you have to advance the pointer i beyond the duplicate. Use a while loop that checks if list[i] == list[i+1] then i++ (before your 2nd dfs call)
      Regarding your second solution, you have an i == l_nums check. If you remove that but keep the append, then you don't need the 2nd backtrack call. The reason why is that in this problem, we want every possible combination, not just the combinations that include the last number.

    • @VarunMittal-viralmutant
      @VarunMittal-viralmutant 2 роки тому

      @@halahmilksheikh I understand the while loop check for the first solution. But didn't understand the i==l_num check in the second

    • @VarunMittal-viralmutant
      @VarunMittal-viralmutant 2 роки тому +2

      @@halahmilksheikh Ohk !! I got it now, after drawing the decision tree on whiteboard. You meant this:
      def backtrack(i, curr_elems):
      # Append the current set of elements without adding anything
      combos.append(tuple(curr_elems))

      prev = None
      elems = curr_elems[:]
      for index, num in enumerate(nums[i:], start=i):
      if num == prev:
      continue
      prev = num
      elems.append(num)
      backtrack(index+1, elems)
      elems.pop()
      -------------------------
      Yes, this works too. It's just the eyes/brain is so used to having a 'return' statement in backtracking solution that without it, it just freaks out :P

  • @karthiklv29
    @karthiklv29 3 роки тому +1

    Thanks for the drawing followed by code flow, very well explained

  • @hithambasheir3283
    @hithambasheir3283 Рік тому +3

    I did something a little bit different here, when I drew the graph, I noticed that the duplicate values will always be next to each other in the result, so I did a comparison between the latest element in the solution and the current subset, worked like charm!
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
    nums.sort()
    res = []
    sub = []
    def dfs(depth):
    if depth >= len(nums):
    res.append(sub[:])
    return
    sub.append(nums[depth])
    if not res or res[-1] != sub:
    dfs(depth+1)
    sub.pop()
    if not res or res[-1] != sub:
    dfs(depth+1)
    dfs(0)
    return res

  • @debanjandas1378
    @debanjandas1378 Місяць тому

    It's the best explanation!

  • @combatLaCarie
    @combatLaCarie 9 місяців тому

    I believe the children under the first [2] should be blue. Love the vid, just posting in case someone else has the same question/concern/thought.

  • @John-po9wz
    @John-po9wz 2 роки тому +4

    could you not just sort the nums array then store every subset in a set to take care of duplicates? since any duplicate would appear the same from the sorted tree (ex. [1,2]. [1,2] instead of [1,2], [2,1] )

    • @AbhishekSharma-vb9ux
      @AbhishekSharma-vb9ux Рік тому +3

      I used this approach, but soon realized that it would fail for larger test cases, i.e where the nums contain more than 10 elements.. I know this question has a constraint of length < 10, but in general it would fail. It gives TLE.

    • @Arthurgarzajr
      @Arthurgarzajr Рік тому

      @@AbhishekSharma-vb9ux true for bigger lengths it would take longer, but we are given constraints.

  • @orangethemeow
    @orangethemeow 3 роки тому

    Favorite LC channel

  • @ahmedanwer6899
    @ahmedanwer6899 11 місяців тому

    at 2:48, the time complexity isn't (2^n)n right? that's the space complexity and time is 2^n?
    edit: oh nvm because in each dfs, u are doing arr.copy() which leads us to have the n

  • @adityaraj5200
    @adityaraj5200 2 роки тому

    thanks for the video. I was struggling to understand this.

  • @danielsun716
    @danielsun716 2 роки тому

    time limit exceeded.
    perhaps, the while loop cost too much
    res = []
    nums.sort()
    def backtrack(i, subSet):
    res.append(subSet[::])
    for j in range(i, len(nums)):
    if j > i and nums[j] == nums[j - 1]:
    continue
    subSet.append(nums[j])
    backtrack(j + 1, subSet)
    subSet.pop()
    backtrack(0, [])
    return res
    This solution could work well

  • @ramS-mc8gi
    @ramS-mc8gi 24 дні тому

    what if there is something like [1,2,3,2,4] sorting it and ignoring multiple 2 will never lead to a valid subset 1,3,2,4. since order is important should we sort ?
    Found the answer : A subset of a set is any collection of elements that are taken from the original set, regardless of the order.

  • @sabaamanollahi5901
    @sabaamanollahi5901 2 роки тому

    Thanks for all the videos,

  • @vyankateshkulkarni4374
    @vyankateshkulkarni4374 Рік тому

    thanks for such a great explanation brother

  • @cosepeter2197
    @cosepeter2197 2 роки тому +1

    wondering why I'm getting a "list out of range error" when I rearrange the while conditions 🤔

    • @niloofaryousefi1757
      @niloofaryousefi1757 2 роки тому +2

      I think we get "index out of bound" error when we change the order because i+1 gets out of bound and we still try to access the number in nums[i+1].

    • @cosepeter2197
      @cosepeter2197 2 роки тому

      @@niloofaryousefi1757 yes . Thanks for that

  • @javeshonthego9634
    @javeshonthego9634 Рік тому

    great logic man!

  • @dhruvvaze8754
    @dhruvvaze8754 Місяць тому

    Why is subset passed in as an argument to the recursive function? Seems like the code works without it, since each recursive stack creates a new subset variable.

  • @vijayakumareyunni6010
    @vijayakumareyunni6010 2 роки тому

    Excellent explanation

  • @라로더
    @라로더 8 місяців тому

    class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
    nums = sorted(nums)
    length = len(nums)
    res = []

    subsets = []
    def dfs(i):
    if i >= length:
    subsets.append(res.copy())
    return 1
    res.append(nums[i])
    dfs(i+1)
    res.pop()
    while i+1 < length and nums[i] == nums[i+1]:
    i += 1
    dfs(i+1)
    dfs(0)
    return subsets

  • @KM-zd6dq
    @KM-zd6dq 6 місяців тому

    I think if you are new to leetcode maybe just use a set to do memorization, you might have a easier time

  • @TanishBansal-ss2ou
    @TanishBansal-ss2ou 8 місяців тому

    based of subset 1 and skipping the same elements on second path
    class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
    sub, subs = [], []
    nums.sort()
    def backtrack(i):
    if i >= len(nums):
    return subs.append(sub.copy())
    sub.append(nums[i])
    backtrack(i+1)
    sub.pop()
    while i + 1 < len(nums) and nums[i+1] == nums[i]:
    i += 1
    backtrack(i+1)
    backtrack(0)
    return subs

  • @imajt5
    @imajt5 2 роки тому

    Thanks for the explaination

  • @shriharikulkarni3986
    @shriharikulkarni3986 2 роки тому +1

    Hi neetcode, we could've filtered the input and remove duplicates and then find subsets also?

    • @sujithkumar1997
      @sujithkumar1997 2 роки тому +8

      No. If the list is (1,2,2), removing 2 will not give the subsets (2,2) and (1,2,2).

  • @Virtualexist
    @Virtualexist 6 місяців тому

    why does it happent that we include [2,2] ? Why did this loop not skip the second '2' when it was the same as previous element?

    • @aaravmishra3487
      @aaravmishra3487 3 місяці тому +1

      That's where you should consider drawing the decision tree by yourself and you would understand why [2,2] is being added to the result, That's the case while branching out at index 0 you didn't consider 1 and included the other 2 indices i.e. 1 & 2... check out 7:36

  • @dusvn1484
    @dusvn1484 2 місяці тому

    I take same appoarch from subsets I with tiny update.
    My appoarch is to also sort but make a set rather then list where will be stored currently processed values.
    After sorting values and make this same appoarch we got some duplicate leaf nodes but it's no matter becouse they are not in set at the end.
    After that processing just loop thought results and convert tuples to list and return result.
    class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
    nums = sorted(nums)
    res = set()
    temp = []
    l = len(nums)
    def dfs(index):
    if index==l:
    res.add(tuple(temp.copy()))
    return

    dfs(index+1)
    temp.append(nums[index])
    dfs(index+1)
    temp.pop()
    dfs(0)
    return [list(element) for element in res]

  • @niloofaryousefi1757
    @niloofaryousefi1757 2 роки тому +1

    Great explanation, as always :) just wondering why I get "index out of range" error, when I rearrange the conditions of the while loop 🤔

    • @niloofaryousefi1757
      @niloofaryousefi1757 2 роки тому +1

      I think we get "index out of bound" error when we change the order because i+1 gets out of bound and we still try to access the number in nums[i+1].

    • @eess5690
      @eess5690 2 роки тому +1

      "i+1

  • @chowtuk
    @chowtuk Рік тому +1

    sooooooooooooo good, subset2 make how backtrack works in subset1 more clear

  • @SM-si2ky
    @SM-si2ky 2 роки тому

    Thank You!

  • @ssuriset
    @ssuriset 5 місяців тому

    In one world, there is a 2. In another world, the 2 don't exist!

  • @abhishekpawar8458
    @abhishekpawar8458 2 роки тому

    this is pretty similar to unbounded knapsack :)

  • @peanutbutter785
    @peanutbutter785 2 роки тому +2

    why complexity is o(n*2^n) ? not o(2^n)?

  • @abdullah_ahmed_sherdy
    @abdullah_ahmed_sherdy Рік тому

    why we have to sort our input list

  • @aerialbaz3802
    @aerialbaz3802 2 роки тому +1

    Why backtrack when you can do this
    class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
    nums.sort()
    result = [[], [nums[0]]]
    len_result = 1
    for i in range(1,len(nums)):
    len_result_prev = len_result
    len_result = len(result)

    if nums[i-1] != nums[i]:
    for j in range(len_result):
    res_copy = result[j].copy()
    res_copy.append(nums[i])
    result.append(res_copy)
    else:
    for j in range(len_result_prev,len_result):
    res_copy = result[j].copy()
    res_copy.append(nums[i])
    result.append(res_copy)

    return result

  • @SabinBajracharya
    @SabinBajracharya 2 роки тому

    Using set to remove duplicates
    fun subsetsWithDup(nums: IntArray): List {
    fun dfs(nums:IntArray, index: Int): List {
    if (index == 0) {
    return listOf(listOf(nums.first()), listOf())
    }
    val res = dfs(nums, index - 1)
    return res + res.map { (it + nums[index]) }
    }
    return dfs(nums.sorted().toIntArray(), nums.lastIndex).toSet().toList()
    }

  • @Ari-ej6lb
    @Ari-ej6lb Рік тому

    Why does it need to be sorted?

  • @balajinagappan1311
    @balajinagappan1311 2 роки тому +1

    Why are you doing " res.append(subset[ :: ]) " and not just " res.append(subset[ :: ]) " . What is the difference between them

    • @HalfJoked
      @HalfJoked 2 роки тому +2

      Looked into this pretty extensively. I don't think there is a difference. Try running both, passes all test cases. So does subset.copy() and runs in the same amount of time and space (run it multiple times). Personally I like .copy() because it makes it more explicit as to what's going on, the whole [:] and [::] syntax is too ambiguous IMO.

  • @polomarco7762
    @polomarco7762 Рік тому +4

    Can somebody explain me why the TC is O(N*2^N)?

    • @arthmetic456
      @arthmetic456 Рік тому +1

      2 because for each element, we have 2 options - include it or don't, 2^n because there are n elements in the input array. and we multiply this with n because the length of the longest subset is that array itself(with n elements)....so we always take the largest subset size when it comes to TC. hope this helps!

  • @rugvedb9842
    @rugvedb9842 2 роки тому

    Thanks a lot. helps so much!!

  • @PippyPappyPatterson
    @PippyPappyPatterson Рік тому

    On line 8, why use `res.append(subset[::])` and not `res.append(subset[:])`?

    • @peskovdev
      @peskovdev Рік тому

      No reason, just his preferences

  • @tinymurky7329
    @tinymurky7329 Рік тому

    I used brute force way, it is kinda fast, 73% Runtime and 91% memory
    class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
    nums.sort()
    dp = set([()])
    for n in nums:
    nextDp = dp.copy()
    for subset in dp:
    nextSub = subset + tuple([n])
    nextDp.add(nextSub)
    dp = nextDp
    return list(dp)

  • @placidnick100
    @placidnick100 3 роки тому

    great, you don't need to pass 'subset' as an argument.

  • @zarifmahfuz96
    @zarifmahfuz96 2 роки тому

    It wasn't clear why the input array needs to be sorted. Could you make that more clear? :)

    • @alexandros27.
      @alexandros27. Рік тому +2

      sorting can make the execution of the while loop part easier. it is easier to increment the indices of dulicate elements when the array is sorted

  • @_deepvision
    @_deepvision Рік тому

    good video

  • @FireAspectPL
    @FireAspectPL Місяць тому

    I hate this question. It just makes no sense to me to treat duplicate values as unique when they're in a subset together, but treat them as duplicates when they're not

  • @sleepypanda7172
    @sleepypanda7172 2 роки тому

    Ooooooooooooh now I got it!

  • @PippyPappyPatterson
    @PippyPappyPatterson Рік тому

    Why is the time complexity the number of combinations times the length of the longest combination? I.e. why `n * n!` instead of `n!`?

    • @matthewsaucedo2471
      @matthewsaucedo2471 Рік тому +1

      The total number of recursive function calls will be 2 ^ n - additionally, at each function call a deep copy of the subset generated so far is created and added to the subsets list. This will incur an additional O(n) time (as the maximum number of elements in the currentSubset will be n). So overall, the time complexity of this approach will be O(n⋅2^n).

    • @PippyPappyPatterson
      @PippyPappyPatterson Рік тому

      @@matthewsaucedo2471
      > at each function call a deep copy of the subset generated so far is created and added to the subsets list.
      This only occurs at the leaf nodes, not every recursion.

    • @matthewsaucedo2471
      @matthewsaucedo2471 Рік тому +2

      @@PippyPappyPatterson
      Hm - this is a good point. I suppose in the worst case, each of your recursive calls resulting in a call that spawns a copy?

    • @PippyPappyPatterson
      @PippyPappyPatterson Рік тому +1

      @@matthewsaucedo2471 hmm yeah but then it would be flat and would be `O(n * 2)` or, if even half the calls spawned a copy, `O(n/2 * 4)`.
      Continuing on in that series:
      `O(n/4 * 8)`, `O(n/8 * 16)`, …
      Seems like it would always reduce down to O(n * 2) or O(n/∞ * nodeCount).
      ---
      TBH, I also don't really understand why `leafCount == n!` and not just `2^n`. I need to get better at complexity analysis - seems like people get _real_ hand-wavey about it if it's ever at all complicated.

  • @casanova5005
    @casanova5005 2 роки тому +1

    I do not get why is it n*2^n shouldn't it be just 2^n

    • @abhishekc3556
      @abhishekc3556 2 роки тому +2

      Considering each subset you generate is of average size n, you'd require O(n) time to append it to your list. I guess this is why we have that extra n.

    • @PippyPappyPatterson
      @PippyPappyPatterson Рік тому

      @@abhishekc3556 Decent rational. Danke shaye, shambala, inshalla, and mariskahargitay.

    • @abhishekc3556
      @abhishekc3556 Рік тому

      @@PippyPappyPatterson whaaat??

    • @PippyPappyPatterson
      @PippyPappyPatterson Рік тому

      @@abhishekc3556 I share my culture with you.

    • @abhishekc3556
      @abhishekc3556 Рік тому

      @@PippyPappyPatterson cool

  • @madtoiletpaper
    @madtoiletpaper 3 роки тому +1

    Also could you do a video on Leetcode #523 - Continuous Subarray Sum? I know its similar to Subarray Sum Equals K, but I find it a little harder to understand.

    • @jugsma6676
      @jugsma6676 2 роки тому

      I came up with this, but still some test cases didn't pass, if u figured it out do let me know,
      def checkSubarraySum(nums: List[int], k: int) -> bool:
      if len(nums) < 2: return False
      if len(nums) == 2 : return True if nums[0]+nums[1] % k == 0 else False
      for i in range(1,len(nums)):
      sum = 0
      j = i
      while j >= 0:
      sum += nums[j]
      if sum % k == 0:
      return True
      j -= 1
      return False

  • @OliverBoyd-my2ie
    @OliverBoyd-my2ie Місяць тому

    Kinda confused: my solution takes 1ms but is completely bruteforce and only beats 37%... using Python3:
    class Solution(object):
    def subsetsWithDup(self, nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    nums.sort()
    def dfs(stuff):
    storage = []
    if not stuff:
    return [[]]
    final = stuff.pop()
    rest = dfs(stuff)
    for element in rest:
    new_element = element.copy()
    new_element.append(final)
    if element not in storage:
    storage.append(element)
    storage.append(new_element)
    return storage
    return dfs(nums)

  • @-Corvo_Attano
    @-Corvo_Attano 2 роки тому

    Thanks a lot..

  • @milesba4
    @milesba4 Рік тому

    This is a similar pattern to combination sum

  • @dumbfailurekms
    @dumbfailurekms Рік тому

    if i >= len(nums):
    if sorted(lst[:]) not in res:
    res.append(sorted(lst[:]))
    rest of the code same as subsets 1
    way worse time complexity since u sort every time LOL but just thought it was funny

  • @unnhao
    @unnhao 5 місяців тому

    backtrack is so hard ==

  • @chowtuk
    @chowtuk Рік тому

    soooooooooooooooooo good

  • @dipanshu-singh
    @dipanshu-singh Рік тому

  • @masternobody1896
    @masternobody1896 3 роки тому +5

    Lol.too hard to understand

  • @АлексейБлохин-ы8ш
    @АлексейБлохин-ы8ш 10 місяців тому

    just put it here:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
    nums.sort()
    subs = [[]]
    end = 0
    for i in range(len(nums)):
    same_num = i > 0 and nums[i] == nums[i - 1]
    start = 0 if not same_num else end # skip duplicates
    end = len(subs)
    for j in range(start, end):
    subs.append(subs[j] + [nums[i]])
    return subs