-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubsetsII.kt
50 lines (39 loc) · 1.35 KB
/
SubsetsII.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package problems
import java.util.ArrayList
//all combination of set, subset, subsequence
class SubsetsII {
//todo: https://www.youtube.com/watch?v=xXKL9YBWgCY
fun subsetsWithDupIterative(nums: IntArray): List<List<Int>> {
val length = nums.size
val end = 1 shl length
val set : MutableSet<List<Int>> = hashSetOf()
nums.sort()
for (mark in 0 until end) {
val subsequence = ArrayList<Int>()
for (i in 0 until length) {
if ((1 shl i and mark) != 0) {
subsequence.add(nums[i])
}
}
set.add(subsequence)
}
return set.toList()
}
//could be optimized with memoization if this was a recursion
fun subsetsWithDupRecursive(nums: IntArray): List<List<Int>> {
val set : MutableSet<ArrayList<Int>> = hashSetOf()
nums.sort()
rec(0, nums, ArrayList<Int>(), set)
return set.toList()
}
private fun rec (index: Int, nums: IntArray, holder: ArrayList<Int>, set : MutableSet<ArrayList<Int>>) {
set.add(holder)
if (index == nums.size) {
return
}
val currNum = nums[index]
val newHolder = ArrayList(holder).apply { add(currNum) }
rec(index + 1, nums, newHolder, set)
rec(index + 1, nums, holder, set)
}
}