Advertisement
1988coder

90. Subsets II

Jan 2nd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. /**
  2.  * Backtracking (Recursion)
  3.  *
  4.  * T(n) = 0 × (n C 0) + 1 × (n C 1) + 2 × (n C 2) + … + n × (n C n)
  5.  *
  6.  * Note that (n C k) = (n C n-k). Therefore:
  7.  *
  8.  * T(n) = 0 × (n C n) + 1 × (n C n-1) + 2 × (n C n-2) + … + n × (n C 0)
  9.  *
  10.  * If we add these two together, we get
  11.  *
  12.  * 2T(n) = n × (n C 0) + n × (n C 1) + … + n × (n C n)
  13.  *
  14.  * = n × (n C 0 + n C 1 + … + n C n)
  15.  *
  16.  * As per binomial theorem, (n C 0 + n C 1 + … + n C n) = 2^n, so
  17.  *
  18.  * 2*T(n) = n * 2^n => T(n) = n * 2^(n-1) = O(n * 2^n)
  19.  *
  20.  * Time Complexity: O(N * 2 ^ N) Refer https://stackoverflow.com/a/20711498
  21.  *
  22.  * Space Complexity: O(N) (Excluding the result space)
  23.  *
  24.  * N = Length of input nums array
  25.  */
  26. class Solution {
  27.     public List<List<Integer>> subsetsWithDup(int[] nums) {
  28.         List<List<Integer>> result = new ArrayList();
  29.         if (nums == null) {
  30.             return result;
  31.         }
  32.         if (nums.length == 0) {
  33.             result.add(new ArrayList<Integer>());
  34.             return result;
  35.         }
  36.  
  37.         // Since the array can have duplicate numbers, thus we sort the array to bring
  38.         // all duplicates together.
  39.         Arrays.sort(nums);
  40.  
  41.         backtrack(result, new ArrayList<Integer>(), nums, 0);
  42.  
  43.         return result;
  44.     }
  45.  
  46.     private void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums, int start) {
  47.         list.add(new ArrayList<Integer>(tempList));
  48.         for (int i = start; i < nums.length; i++) {
  49.             if (i > start && nums[i] == nums[i - 1]) {
  50.                 continue;
  51.             }
  52.             tempList.add(nums[i]);
  53.             backtrack(list, tempList, nums, i + 1);
  54.             tempList.remove(tempList.size() - 1);
  55.         }
  56.     }
  57. }
  58.  
  59. /**
  60.  * Iterative Solution
  61.  *
  62.  * Time Complexity: O(N * 2 ^ N) Refer https://stackoverflow.com/a/20711498
  63.  *
  64.  * Space Complexity: O(1) (Excluding the result space)
  65.  *
  66.  * N = Length of input nums array
  67.  */
  68. class Solution {
  69.     public List<List<Integer>> subsetsWithDup(int[] nums) {
  70.         List<List<Integer>> result = new ArrayList();
  71.         if (nums == null) {
  72.             return result;
  73.         }
  74.         result.add(new ArrayList<Integer>());
  75.         if (nums.length == 0) {
  76.             return result;
  77.         }
  78.  
  79.         // Since the array can have duplicate numbers, thus we sort the array to bring
  80.         // all duplicates together.
  81.         Arrays.sort(nums);
  82.  
  83.         for (int i = 0; i < nums.length; i++) {
  84.             int count = 1;
  85.             while (i < nums.length - 1 && nums[i] == nums[i + 1]) {
  86.                 count++;
  87.                 i++;
  88.             }
  89.             int listSize = result.size();
  90.             for (int j = 0; j < listSize; j++) {
  91.                 List<Integer> tempList = new ArrayList<Integer>(result.get(j));
  92.                 for (int k = 0; k < count; k++) {
  93.                     tempList.add(nums[i]);
  94.                     result.add(new ArrayList<Integer>(tempList));
  95.                 }
  96.             }
  97.         }
  98.  
  99.         return result;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement