208. Tall People

A group of people stands before you arranged in rows and columns. Looking from above, they form an R by C rectangle of people. Your job is to return 2 specific heights – the first is computed by finding the shortest person in each row, and then finding the tallest person among them (the “tallest-of-the-shortest”); and the second is computed by finding the tallest person in each column, and then finding the shortest person among them (the “shortest-of-the-tallest”).

import java.util.Arrays;

public class TallPeople {
    private void getPeople(String[] people){
        int[] tp = new int[2];
        tp[0] =tp[1]=0;
        int ca=0;
        for (int i=0;i<people.length;i++) {
            String row[] = people[i].split(" ");
            int trow = Integer.MAX_VALUE;
            ca = row.length;
            for (int j=0;j<row.length;j++) {
                int jr =Integer.parseInt(row[j]);
                if(jr<trow) trow= jr;
            }
            if (trow>tp[0]) tp[0] =trow;
            //System.out.println(Arrays.toString(row) + "\t" + trow);
        }

        int tcol[] = new int[ca];
        for (int i=0;i<people.length;i++) {
            String row[] = people[i].split(" ");
            for (int j=0;j<row.length;j++) {
                int jr =Integer.parseInt(row[j]);
                if(jr>tcol[j]) tcol[j] = jr;
            }
            //System.out.println(Arrays.toString(tcol) + "\t" + i);
        }
        tp[1] = Integer.MAX_VALUE;
        for (int i=0;i<tcol.length;i++) {
            if (tcol[i]<tp[1]) tp[1] =tcol[i];
        }
        //System.out.println(Arrays.toString(tcol));
        System.out.println(Arrays.toString(tp));
    }

    public static void main(String[] args) {
        TallPeople tp = new TallPeople();
        tp.getPeople(new String[]    {"9 2 3", "4 8 7"}         );
        tp.getPeople(new String[]    {"1 2", "4 5", "3 6"}         );
        tp.getPeople(new String[]    {"1 1", "1 1"}         );
        tp.getPeople(new String[]    {"1 2", "2 1"}         );
        tp.getPeople(new String[]    {"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20"}         );
        tp.getPeople(new String[]    {"1 1000", "1000 1"}         );
        tp.getPeople(new String[]    {"1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000", "1 1000"}         );
        tp.getPeople(new String[]    {"1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5", "3 4 1 9 8 2 3 4 1 4 5 8 4 1 1 3 4 5 7 8 3 4 2"}         );
    }
}
Advertisement

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: