[英]I am facing timeout for my solution for a HackerRank
I am solving a problem on hackerrank Hacker Rank ICPC Team Problem我正在解决关于 hackerrank Hacker Rank ICPC Team Problem的问题
I have created the following code as solution for problem.我创建了以下代码作为问题的解决方案。
import java.math.BigInteger;
import java.util.Scanner;
public class ACMICPCTeam {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),m=sc.nextInt(),count=0,maxCount=0,teams=0;
sc.nextLine();
String subjectArray[]=new String[n];
for(int i=0;i<n;i++){
subjectArray[i]=sc.nextLine();
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
String temp=""+(new BigInteger(subjectArray[i]).add(new BigInteger(subjectArray[j])));
//System.out.println(temp);
count=temp.replace("0","").length();
if(count>maxCount)
{
maxCount=count;
teams=1;
}
else if(count==maxCount)
{
teams++;
}
}
}
System.out.println(maxCount);
System.out.println(teams);
sc.close();
}
}
So what I am trying to do is I am adding the two teams subjects and I am counting non-zeros of resultant string.所以我想做的是添加两个团队的主题,并计算结果字符串的非零值。 The highest count is number of subjects and the occurrence of highest counts are teams which know max number of subject.
最高计数是受试者的数量,最高计数的出现是知道最大受试者数量的团队。 Even after spending a lot time I am not able to any better solution than this one still I am facing time out as it is not efficient.
即使花了很多时间我也无法找到比这个更好的解决方案,但我仍然面临超时,因为它效率不高。
I have gone through forum of the question but it was of no help.我已经浏览了这个问题的论坛,但没有帮助。
Don't use string logic for this.不要为此使用字符串逻辑。
Parse the string into aBitSet
, before entering your loops, ie as you read them.将字符串解析为
BitSet
,然后再进入您的循环,即在您阅读它们时。
Then use methods or(BitSet set)
, and cardinality()
.然后使用方法
or(BitSet set)
和cardinality()
。
I just completed challenge doing that.我刚刚完成挑战。 No timeouts.
没有超时。
Your solution is not optimal you should try something better.您的解决方案不是最佳的,您应该尝试更好的方法。
You can utilize BigInteger method or BitSet class to make it easy.您可以使用 BigInteger 方法或 BitSet 类来简化操作。
For forming a team you have to use bitwise OR要组建团队,您必须使用按位或
Here are solutions--以下是解决方案——
// 1st approach
static int[] acmTeam(String[] topic) {
int n = topic.length;
BigInteger[] bi = new BigInteger[n];
for (int i = 0; i < n; i++)
bi[i] = new BigInteger(topic[i], 2);
int maxTopic = 0;
int teamCount = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
BigInteger iuj = bi[i].or(bi[j]);
int bitCount = iuj.bitCount();
if (bitCount > maxTopic) {
maxTopic = bitCount;
teamCount = 1;
} else if (bitCount == maxTopic) {
teamCount++;
}
}
}
int result[] = { maxTopic, teamCount };
return result;
}
// 2nd approach--using java BitSet class
static int[] acmTeamUsingBitSet(String[] topic) {
int teamCount = 0, maxTopic = 0;
int size = topic.length;
BitSet[] bitset = new BitSet[size];
for (int i = 0; i < size; i++) {
BigInteger b1 = new BigInteger(topic[i], 2);
bitset[i] = BitSet.valueOf(b1.toByteArray());
}
for (int i = 0; i < size - 1; i++) {
BitSet bitset1 = bitset[i];
for (int j = i + 1; j < size; j++) {
BitSet bitset2 = bitset[j];
BitSet tmpset = new BitSet();
tmpset.or(bitset1);
tmpset.or(bitset2);
if (tmpset.cardinality() > maxTopic) {
maxTopic = tmpset.cardinality();
teamCount = 1;
} else if (maxTopic == tmpset.cardinality()) {
teamCount++;
}
}
}
int result[] = { maxTopic, teamCount };
return result;
}
You can refer this link for a detailed video explanation .您可以参考此链接以获得详细的视频说明。
I got good result using Java 8.我使用 Java 8 得到了很好的结果。
static int[] acmTeam(String[] topic) {
List<List<Integer>> res = IntStream.range(0, topic.length)
.mapToObj(s -> IntStream.range(0, topic[s].length()).boxed()
.collect(Collectors.groupingBy(i -> topic[s].charAt(i))))
.map(m -> m.get('1'))
.collect(toList());
long maxTopic = 0;
int teamCount = 0;
for (int i = 0; i < res.size(); i++) {
for (int j = i + 1; j < res.size(); j++) {
long topics = Stream.concat(res.get(i).stream(), res.get(j).stream()).distinct().count();
if (topics > maxTopic) {
maxTopic = topics;
teamCount = 1;
} else if (topics == maxTopic) {
teamCount++;
}
}
}
return new int[]{(int) maxTopic, teamCount};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.