简体   繁体   English

我的 HackerRank 解决方案正面临超时

[英]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.

相关问题 为什么我在Hackerrank上的这段代码由于超时而被终止? 我的最后一次迭代没有发生? - Why i am getting Terminated due to timeout in this code on Hackerrank? My last iteration is not taking place? Java HackerRank 中的 If-Else,我的代码好还是我可以获得更好的问题解决方案 [关闭] - Java If-Else in HackerRank, Is my code good or can I get better solution for the problem [closed] 为什么我的代码在hackerrank上会收到“因超时而终止”错误? - Why do i get a "terminated due to timeout" error for my code at hackerrank? 运行我的应用程序时,我在 Flutter 中遇到问题 - I am facing issue in Flutter while run my application 当我在 HackerRank 中提交我的代码时,它在三个测试用例中失败并显示“由于超时而终止:(”。那么我该如何解决呢? - When I submit my code in HackerRank, it failed in three test cases and showing that "Terminated due to timeout :(" .So how can I fix it? 运行我的Java代码时,我遇到此错误-&gt; - when running my java code i am facing this error -> Hackerrank Mark 和 Toys 质疑我的解决方案不适用于大型输入测试用例 - Hackerrank Mark and Toys Question my solution not working for large input testcases Hackerrank动态数组超时 - Hackerrank Dynamic Array Timeout Hackerrank不接受有效的解决方案 - Hackerrank not accepting valid solution 我在 Android 中面临 ClassNotFoundException - I am facing ClassNotFoundException in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM