0
\$\begingroup\$

It solves the problem listed here. How can I make this more efficient?

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static int getIndex(int x, int lastAns,Integer size) {
        return ((x ^ lastAns) % size);
    }

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int q = in.nextInt();

    LinkedList<Integer>[] sequenceList = new LinkedList[n];
    int lastAns = 0;
    while(q-- != 0) {
        int queryType = in.nextInt();
        int x = in.nextInt();
        int y = in.nextInt();
        int index = getIndex(x, lastAns, n);
        if(queryType == 1) {
            if(sequenceList[index] == null) sequenceList[index] = new LinkedList<Integer>();
            sequenceList[index].add(y);
        } else if(queryType == 2) {
            lastAns = sequenceList[index].get(y % sequenceList[index].size());
            System.out.println(lastAns);
        }
    }
  }
}
\$\endgroup\$
2
  • 4
    \$\begingroup\$ Include the problem statement into your question's body. \$\endgroup\$
    – Denis
    Feb 6, 2017 at 23:51
  • \$\begingroup\$ @denis It solves the problem listed here is the link, highlighted. Mentioned just before the code. thank you \$\endgroup\$
    – CodeShadow
    Feb 7, 2017 at 6:48

1 Answer 1

1
\$\begingroup\$
  1. When instancing generic collections, it' best to use the diamond operator to let the compiler figure out the generic arguments. This lets you define in a single place (type definition, not constructor) for easier future modification.

    sequenceList[index] = new LinkedList<>();

  2. When using an Object, always declare its type as the highest class in its type hierarchy (up to interface if possible).

You're not using LinkedList's specific methods, so:

List<Integer>[] sequenceList = new List[n]; 
  1. Javadoc!

  2. Use proper Objects:

    • Make Solution a real instance created one in main (no real job should be performed in main). Then you can make lastAns and n fields of Solution because in the problem statement it is made to be a state of the Solution, so you don't need to pass those around every time
    • You could Wrap your Lists in a Sequence internal class of Solution. Internal means it will be able to access the lastAns field of the Solution instance etc.
  3. It's a bit weird to lazily initialize the sequences. You don't win much, but you put an ugly if in your code. If there is no risk having a huge but sparse array, I would drop this.

  4. Don't one-line ifs, and always use brackets

  5. I'm usually against calling variables x, y etc. On this case it's defined in the problem so it's ok.


Updated code:

public class Solution {
    private int n;
    private int q;
    private int lastAns = 0;
    public Solution(int size, int numberOfQuestions) {
        n = size;
        q = numberOfQuestions;
    }
    public int getIndex(int x) {
        return (x ^ lastAns) % n;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Solution solution = new Solution(in.nextInt(), in.nextInt());
        solution.solve(in);
    }
    public void solve(Scanner in) {
        List<Integer>[] sequenceList = new List[n];
        while(q-- != 0) {
            int queryType = in.nextInt();
            int x = in.nextInt();
            int y = in.nextInt();
            int index = getIndex(x);
            if(queryType == 1) {
                if(sequenceList[index] == null) {
                    sequenceList[index] = new LinkedList<Integer>();
                }
                sequenceList[index].add(y);
            } else if(queryType == 2) {
                lastAns = sequenceList[index].get(y % sequenceList[index].size());
                System.out.println(lastAns);
            }
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.