由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - subset
相关主题
请教一下subset I 输出子集顺序问题a problem from leetcode: high efficiency algorithm for combinations problem
问道老题combinations 有没有 iterative的方法阿 ?
LeetCode 的 4 sum 问题 如何用hash table做呢?问一个G家面试题
问一道算法题MS intern 电面被拒,附上面试过程
这个题咋做?一道onsite面试题
问一道题(1)Facebook Phone Inteview + 流程请教
请教一个题目amazon 一道题
请教leetcode Subsets II问个算法题2
相关话题的讨论汇总
话题: set话题: integer话题: sets话题: hashmap话题: subset
进入JobHunting版参与讨论
1 (共1页)
r*******y
发帖数: 1081
1
givn m sets. some sets may be subsets of others. If one set is a subset of
another set, then this smaller set will be removed. Finally we have the sets
from which we can not remove any set. Assume the elements in all the sets
are
same type and only "==" operator is supported on these elements.
Is there any good algorithm to do this job ? Thanks.
g**********y
发帖数: 14569
2
基本想法:把Element映射到数字,然后用Bitmap来计算包容关系。
pesudo-code =>
foreach set in sets {
Bitmap b1 = transform(set);
boolean add = true;
foreach b2 in map.keySet() {
Bitmap b3 = b1.and(b2);
if (b3.equals(b2)) {
bitmaps.remove(b2);
}
else if (b3.equals(b1)) {
add = false;
}
}
if (add) map.put(b1, set);
}
return map.values();
r*******y
发帖数: 1081
3
So it is using hashing. But how to do hashing here is also a big deal, right
?
Thanks

【在 g**********y 的大作中提到】
: 基本想法:把Element映射到数字,然后用Bitmap来计算包容关系。
: pesudo-code =>
: foreach set in sets {
: Bitmap b1 = transform(set);
: boolean add = true;
: foreach b2 in map.keySet() {
: Bitmap b3 = b1.and(b2);
: if (b3.equals(b2)) {
: bitmaps.remove(b2);
: }

g**********y
发帖数: 14569
4
I am not sure what's your question. But as example, here is a simplified
Java implementation, assume sets no more than 32. If more than that, you can
use BigInteger or write your own class.
public class Subset {
public Set[] merge(Set[] sets) {
HashMap cardinal = new HashMap();
HashMap map = new HashMap();
HashSet remove = new HashSet();

for (Set set : sets) {
int b1 = transform(set, cardinal);
boolean add = true;
remove.clear();

for (int b2 : map.keySet()) {
int b = b1 & b2;
if (b == b1) {
add = false;
}
else if (b == b2) {
remove.add(b2);
}
}

if (add) map.put(b1, set);
for (Integer b : remove) map.remove(b);
}

Set[] result = new Set[map.size()];
return map.values().toArray(result);
}

private int transform(Set set, HashMap cardinal) {
int n = 0;
for (Object o : set) {
if (!cardinal.containsKey(o)) {
cardinal.put(o, cardinal.size());
}
n |= 1 << cardinal.get(o);
}
return n;
}
}

right

【在 r*******y 的大作中提到】
: So it is using hashing. But how to do hashing here is also a big deal, right
: ?
: Thanks

1 (共1页)
进入JobHunting版参与讨论
相关主题
问个算法题2这个题咋做?
Another DP Problem: Balanced Partition问一道题(1)
请教150上面binary的next permutation请教一个题目
partition array problem请教leetcode Subsets II
请教一下subset I 输出子集顺序问题a problem from leetcode: high efficiency algorithm for combinations problem
问道老题combinations 有没有 iterative的方法阿 ?
LeetCode 的 4 sum 问题 如何用hash table做呢?问一个G家面试题
问一道算法题MS intern 电面被拒,附上面试过程
相关话题的讨论汇总
话题: set话题: integer话题: sets话题: hashmap话题: subset