Frequency Queries

  • + 4 comments

    I kept adjusting my code, and got my solution to O(n), where n = number of queries, and it still didn't pass test case 10 due to time out. Cut out a bunch of fat from the Boilerplate, and finally got it everything to pass with my solution. Here's what I ended up with for my boilerplate:


    public static void main(String[] args) throws IOException {
            try (BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(System.in))) {
                
                int q = Integer.parseInt(bufferedReader.readLine().trim());
                int[][] queries = new int[q][2];
               
                for (int i = 0; i < q; i++) {
                    String[] query = bufferedReader.readLine().split(" ");
                    queries[i][0] = Integer.parseInt(query[0]);
                    queries[i][1] = Integer.parseInt(query[1]);
                }
              
                List<Integer> ans = freqQuery(queries);
              
                try (BufferedWriter bufferedWriter = new BufferedWriter(
                            new FileWriter(System.getenv("OUTPUT_PATH")))) {
                
                    bufferedWriter.write(ans.stream().map(Object::toString)
                                .collect(joining("\n")) + "\n");
                }
            }
        }
    


    Solution method does need to take in int[][] instead of List<List<Integer>>