Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions SubSetII/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
```java
// Backtracking
// Generate all subsets and use a map to remove duplicate
// Sort it frist, if the previous number is same with current number meaning that the subproblem of
// the current number will be already consider in the previous number. So, we skip
// It can reduce the space complextity for the map
// That will increase the time complexity but order wouldn't change because the overal time complexity will depends on biggest order(2 ** n)
// Time complexity: O(2 ** N * N(list copy)) N: number of nums
// Space complexity: O(2 ** N) result
// Time spend: 12:21
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

たぶん、この問題も、backtrack の仕方が複数あるように思います。

class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), nums, 0);
return res;
}

private void backtrack(List<List<Integer>> res, List<Integer> tempList, int[] nums, int start) {
res.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == nums[i - 1]) {
continue;
}
tempList.add(nums[i]);
backtrack(res, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
```
23 changes: 23 additions & 0 deletions SubSetII/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
```java
// Time spend: 02:14
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), nums, 0);
return res;
}

private void backtrack(List<List<Integer>> res, List<Integer> tempList, int[] nums, int start) {
res.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == nums[i - 1]) {
continue;
}
tempList.add(nums[i]);
backtrack(res, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
```
71 changes: 71 additions & 0 deletions SubSetII/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
```java
// Time spend: 02:01
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), nums, 0);
return res;
}

private void backtrack(List<List<Integer>> res, List<Integer> tempList, int[] nums, int start) {
res.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == nums[i - 1]) {
continue;
}
tempList.add(nums[i]);
backtrack(res, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
```

```java
// Time spend: 01:58
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), nums, 0);
return res;
}

private void backtrack(List<List<Integer>> res, List<Integer> tempList, int[] nums, int start) {
res.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == nums[i - 1]) {
continue;
}
tempList.add(nums[i]);
backtrack(res, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
```

```java
// Time spend: 01:53
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), nums, 0);
return res;
}

private void backtrack(List<List<Integer>> res, List<Integer> tempList, int[] nums, int start) {
res.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == nums[i - 1]) {
continue;
}
tempList.add(nums[i]);
backtrack(res, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
```