请教各位大牛一个问题:
Problem: Output all subsets of size of m from an array
Solution Hints: String permutation Problem variant
Python Codes:
def Subset_m(v,l,m,output):
"Find all subsets of an array v of size m, need fixing"
if l==m:
print output
return
for i in range(l,len(v)):
output[l]=v[i]
Subset_m(v,l+1,m,output)
C++ Codes:
/*Description: output all the subset of size m
Input:vector v, int m,int l, vector& output
Output: void
*/
... 阅读全帖
碰上类似的问题,除了用set,总是搞不定。谢谢指教。要排序和用prev吗?
Given a collection of integers that might contain duplicates, S, return all
possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
I use first. and last. in data step. It works well.
I will try sql later.
proc sort data=raw;
by personid;
run;
data subset (drop=count);
set raw;
by personid;
if first.personid then count=1;
else count+1;
if count<=3 then output;
proc print data=subset;
title 'Subset size is 3';
run;
row <- seq(1:100)
row <- sample(row) ## it will randomize the 100 numbers
then you can pick like first n numbers(depends on the size of each subset
you need) as your subset index.
c <- row[1:n]
data[c,]
if you want all the possible sizes of subset i'm afraid you still need to
use loop.
Hope it might help you
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.
没有办法了,只能上这个了
def Subset_m(v,l,m,result):
"Find all subsets of an array v of size m, need fixing"
if l==m:
if sorted(v[:m]) not in result:
print v[:m]
result.append(sorted(v[:m]))
为什么要j>=prevSize?总是不理解这一点,多谢!
if (i==0||S[i]!=S[i-1]||j>=prevSize) https://oj.leetcode.com/problems/subsets-ii/
Given a collection of integers that might contain duplicates, S, return all
possible subsets.
Note:
vector > subsetsWithDup(vector &S) {
vector> ret;
int n = S.size();
if (n==0) return ret;
sort(S.begin(),S.end());
ret.resize(1);
int prevSize = 0;
for (int i=0; i
{
... 阅读全帖
I am using SAS to deal with a huge data file with over 10 millions of
observations. “personid” is a variable. The structure of “personid” is
like, for example, xxxx2, xxxx2, xxxx2, xxxx3, xxx3, xxxx5, xxxx5, xxxx5,
xxxx6, xxxx7……., for a unique “personid”, there are several observations
as shown in the example above. I am trying to get subset that the frequency
of the unique “personid” has certain frequency, say, frequency=3, in the
case of example above, that means I want to obtain a subset:
if there is no duplicate and the order var has no missing, try this
%macro subset;
proc sql;
select count(order) into :n
from data0;
quit;
%do i=1 %to &n.;
%do j=1 %to &n.-&i.+1;
data data&i._&j.;
set data0;
if order>=&j. and order<=&n-&j+1;
run;
%end;
%end;
%mend subset;
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
我怎么觉得是O(n^3)呢 http://stackoverflow.com/questions/12278528/find-subset-with-el
里面ElKamina的答案是对的,不过有一处没说清楚,X[i,j] 应该是 the optimum
result of selecting j elements from the first i elements provided that A[j-1
] is included.
贴个代码
public int cal(int[] a, int k)
{
if (k <2 || a == null ||a.length < 2)
return -1;
Arrays.sort(a);
if (k == 2)
return a[a.length-1] -a[0];
int[][] res = new int[a.length + 1][a.length +1];... 阅读全帖
Steinman实验室的Science文章。非常老的一个抗原,很有意思的发现,非常新的认识
。这一期的Cell上有Akiko Iwasaki写的一篇prereview,记得这个版面有人评论过这个
日本mm是否美女来着,显然她的研究领域已经不仅仅是mucousal immunity了,应该还
会保持上升的。 http://www.sciencemag.org/cgi/content/abstract/315/5808/107
Science 5 January 2007:
Vol. 315. no. 5808, pp. 107 - 111
DOI: 10.1126/science.1136080
Prev | Table of Contents | Next
Reports
Differential Antigen Processing by Dendritic Cell Subsets in Vivo
Diana Dudziak,1 Alice O. Kamphorst,1 Gordon F. Heidkamp,1 Veit R. Buchholz,1
Christine Trump
if I have table like this:
1 1.234
2 1.657
3 1.564
4 2.343
..
,,
the first column is order information and the second column is value.
is there a way to sample all possible continuous subset of x row from the table?
thanks
maybe I did not express myself clear enough,
what I need is a subsets that has continuous row number, the solution
rabbit1860 give is random row number.
the solution libra give is in SAS ? the syntax looks strange to me.
DaShagen, I think it is basically a sliding window of size x and step 1.
我有两个datasets:
first dataset:
Name Response
A *
A *
B *
B *
B *
C *
C *
second dataset:
Name Response
A *
A *
A *
B *
C *
C *
C *
D *
D *
E *
我想subset第二个dataset,让它只留下第一个dataset出现的name的数据,也就是把第
二个dataset里的D和E对应的observations都去掉
请教大家
Subset Selection in Regression这本书讲了不少细节,不过我觉得内容有点老。网上
有电子书。
简单来说GLM的automatic selection都是基于likelihood的,一般来说是likelihood
ratio test with pre-specified significance level,当然也可以用AIC,BIC,
cross validation之类的。
Why you people like to decrement m instead of incrementing m?
By increment, you can get rid of push_back and erase, if you reserve size m
for subset.
Thus you can access subset[m] each time and don't need to erase elements.
题目的意思相当于有序打印集合A所有大小为3的子集,代码如下:
void run(int a[], int n, int m, vector &subset)
{
if(n < m) return ;
if(m == 0) {
for(int i = 0; i < subset.size(); i++)
cout << subset[i] << " ";
cout << endl;
return ;
}
subset.push_bac... 阅读全帖
Using SAS to subset data, I can get N=48, but using R, I can not, I don't
know anything about. Would anybody check my R code and see where is the
problem? Below is the SAS code and R code. Thanks.
data x;
set ALL_12222010;
where tau>93 & abeta142<192 & PTAU181P>23;run; **n=48;
#R code;
sizing <- function(variable.name,dx.group){
cut.sample <- function(variable.name,dx.group){
attach(variable.name)
variable.name$enrich=as.numeric(tau>93 & abeta142<192 & PTAU181P>23);
... 阅读全帖
"In computer science, the subset sum problem is an important problem in
complexity theory and cryptography. The problem is this: given a set of
integers, does the sum of some non-empty subset equal exactly zero? For
example, given the set { −7, −3, −2, 5, 8}, the answer is
yes because
the subset { −3, −2, 5} sums to zero. The problem is NP-complete.
An equivalent problem is this: given a set of integers and an integer s,
does any non-empty subset sum to s? Subset su... 阅读全帖
vector subsets(vector a) {
int n = a.size();
// minimum negative sum
int min = 0;
// max postive sum
int max = 0;
for(int i = 0; i < n; i++) {
if (a[i] < 0) {
min += a[i];
}
else {
max += a[i];
}
// mask[i] = 1 means there is a sub sum equals to i
int mask[] = new int[-min + max + 1];
// lookupTable[i, j] = 1 means there is a sub sum equals to j
// and a[i] is in the sub set
ve... 阅读全帖
import java.util.Hashtable;
public class Solution {
Hashtable Dict = new Hashtable ()
;
public ArrayList letterCombinations(String digits) {
ArrayList res = new ArrayList();
if(digits.length()==0){
res.add("");
return res;
}
Dict.put(2, new String[]{"a", "b", "c"});
Dict.put(3, new String[]{"d", "e", "f"});
Dict.put(4, new String[]{"g", "h", "i"});
... 阅读全帖
Given two integers n and k, return all possible combinations of k numbers
out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
the given api is
public ArrayList> combine(int n, int k)
the solution I got is as follows which is really slow O(n!) and time out at
10,7. So please provide any idea of higher efficiency algorithm for this
one. Thanks in advance
public void combination(ArrayList> r... 阅读全帖
大牛快来看看
Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find
all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending
order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set 10,1,2... 阅读全帖
弄两个数组,A[n], B[n] where n is the number of jobs,然后再用一个变量来存总
cost,如果想把每一步的总cost和每一步的subset size存起来的话,也可以多弄几个
数组。
A[i] 存从0 - i个job的最优解subset的最后一个job的index
B[i] 存A[i]对应的最优解subset的倒数第二个job的index
比如对于一个5个job的输入数组,A[4]=3,表示这5个输入的最优解subset的最后一个
job的index为3, 然后B[4]=2,表示取完3之后要去取2,然后找A[2]=1,然后找B[2],
一路找一下去,时间复杂度是O(k),k是subset的size
I think you can prove (1) by the following arguments:
1.1 It is true when A is any open segment -- This is just by defination of
cdf.
1.2 It is true for any open subset of [0 1]. An open subset is just a
uninon of countably many opens segments.
1.3 It is true for any Borel measurable subset A: m_n(A) = inf_{A\subset D,
D open}m_n(D)<= B\inf_{A\subset D, D open}m(D) = B m(A)