Hackerrank’s Service Lane

I am again posting code without asymtotic analysis. There is no standard algorithm used here. I believe this problem pertains to Range Minimum Query algorithm which I hope to explore.

It is ridiculous that the code to parse the input values is more complex than the actual brute-force algorithm. Should Hackerrank parse it for us ?

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Solution {

    private int noOfTestCases;

    private ArrayList<Integer> widths = new ArrayList<>();
    private ArrayList<Segment> tests = new ArrayList<>();

    OutputStream outputStream = System.out;
    PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));


    public void  getInput(BufferedReader br) throws IOException {
        Scanner s = null;
        int lengthHighway = 0;
        try {
            s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));

            if (s.hasNextLine()) {
                String[] s1 = s.nextLine().split("\\s+");
                lengthHighway = Integer.parseInt(s1[0]);
                noOfTestCases = Integer.parseInt(s1[1]);
            }
            if (s.hasNextLine()) {
                String[] s1 = s.nextLine().split("\\s+");
                for( int i = 0 ; i < s1.length ; i ++){
                    widths.add( Integer.parseInt(s1[i]) );
                }
            }
            while (s.hasNextLine()) {
                String[] s1 = s.nextLine().split("\\s+");
                tests.add( new Segment(Integer.parseInt(s1[0]), Integer.parseInt(s1[1]) ));
            }
        } finally {
            s.close();
        }
    }

    class Segment{

        public final int entry,exit;

        public Segment(int entry, int exit) {
            this.entry = entry;
            this.exit = exit;
        }
    }

    public static void main( String... argv ) throws IOException {
        Solution rm = new Solution();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        rm.getInput(br);
        rm.fit();

    }

   private void fit() {
        int[] vehicle = new int[]{1,2,3};

        try {
            for( Segment test : tests){
                Integer[] a  = Arrays.copyOfRange(widths.toArray(new Integer[widths.size()]), test.entry, test.exit+1);
                Arrays.sort(a);
                pw.println(vehicle[a[0]-1]);
                pw.flush();
            }
        }finally{
            pw.close();
        }
    }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: