代码之家  ›  专栏  ›  技术社区  ›  Csa77 Bartosz Radaczyński

时限超过了最佳方法(Java)

  •  0
  • Csa77 Bartosz Radaczyński  · 技术社区  · 6 年前

    即使我认为我解决了一个有竞争力的程序 problem from HackerEarth 使用最佳方法,所有测试都会超过时间限制。我真的不知道如何进一步优化它,因为这只是一个简单的练习。

    我的方法:迭代所有数组成员,然后将它们添加到 HashMap 存储它们的事件。之后,只需读取查询编号并从 哈希图 .

    这是我的解决方案:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.HashMap;
    import java.util.Map;
    
    class TestClass {
    
        public static void main(String args[]) throws Exception {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int t = Integer.parseInt(br.readLine());
    
            //go through all test cases
            for (int i = 0; i < t; i++) {
                Map<Integer, Integer> map = new HashMap<>();
                String[] inputs = br.readLine().split(" ");
                int N = Integer.parseInt(inputs[0]);
                int Q = Integer.parseInt(inputs[1]);
                inputs = br.readLine().split(" ");
    
                //read array
                for (int j = 0; j < N; j++) {
                    int x = Integer.parseInt(inputs[j]);
                    Integer value = map.get(x);
                    //if number is already in hashmap then increment its count
                    //else put it into the map with a count of 1
                    if (value == null) {
                        map.put(x, 1);
                    } else map.put(x, value + 1);
                }
    
                //iterate through the queries and get their occurences from the map
                for (int j = 0; j < Q; j++) {
                    int x = Integer.parseInt(br.readLine());
                    Integer value = map.get(x);
                    if (value == null) {
                        System.out.println(0);
                    } else System.out.println(value);
                }
            }
        }
    }
    

    我的问题是:我的方法有什么问题?为什么时间用完了?

    2 回复  |  直到 6 年前
        1
  •  1
  •   tomky71    6 年前

    PrinteWriter

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.util.HashMap;
    import java.util.Map;
    
    class TestClass {
    
        public static void main(String args[]) throws Exception {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter pr = new PrintWriter(System.out);
            int t = Integer.parseInt(br.readLine());
    
            //go through all test cases
            for (int i = 0; i < t; i++) {
                Map<Integer, Integer> map = new HashMap<>();
                String[] inputs = br.readLine().split(" ");
                int N = Integer.parseInt(inputs[0]);
                int Q = Integer.parseInt(inputs[1]);
                inputs = br.readLine().split(" ");
    
                //read array
                for (int j = 0; j < N; j++) {
                    int x = Integer.parseInt(inputs[j]);
                    Integer value = map.get(x);
                    //if number is already in hashmap then increment its count
                    //else put it into the map with a count of 1
                    if (value == null) {
                        map.put(x, 1);
                    } else map.put(x, value + 1);
                }
    
                //iterate through the queries and get their occurences from the map
                for (int j = 0; j < Q; j++) {
                    int x = Integer.parseInt(br.readLine());
                    Integer value = map.get(x);
                    if (value == null) {
                        pr.println(0);
                    } else pr.println(value);
                }
            }
            pr.close();
        }
    }  
    

        2
  •  0
  •   Philip DiSarro    6 年前

    import java.util.*;
    class TestClass {
        public static void main(String args[] ) throws Exception {
            Scanner s = new Scanner(System.in);
            int T = s.nextInt();       
            Map<Integer,Integer> map=new HashMap<Integer,Integer>();
            for(int i=0;i<T;i++)
            {
                StringBuilder sb=new StringBuilder();
                int N=s.nextInt();
                int Q=s.nextInt();
                int[] arr=new int[N];
                for(int j=0;j<N;j++)
                {
                    arr[j]=s.nextInt();
                    if(map.containsKey(arr[j]))
                    {
                        map.put(arr[j],map.get(arr[j])+1);
                    }
                    else
                    map.put(arr[j],1);
                }
                for(int k=0;k<Q;k++)
                {
                    int X=s.nextInt();
                    if(map.containsKey(X)){
                        sb.append(map.get(X)+"\n");
                    }
                    else{
                        sb.append(0+"\n");
                    }     
                }
                 System.out.println(sb.toString()); 
                map.clear();
            }
    
        }
    }
    

     String[] inputs = br.readLine().split(" ");
     int N = Integer.parseInt(inputs[0]);
     int Q = Integer.parseInt(inputs[1]);
     inputs = br.readLine().split(" ");
    

    Scanner vs. BufferedReader Integer.parseInt(...) br.readlines().split(" ")

    Integer value = map.get(x);
    if (value == null) {
        pr.println(0);
    } else pr.println(value);
    

    map.containsKey(x)

    inputs = br.readLine().split(" "); Integer.parseInt(..) s.nextInt() br.readLine() parseInt()