Skip to content

Instantly share code, notes, and snippets.

@bhnascar
Created May 1, 2016 04:40
Show Gist options
  • Save bhnascar/fbf72237bf1887faf212aec58274dd80 to your computer and use it in GitHub Desktop.
Save bhnascar/fbf72237bf1887faf212aec58274dd80 to your computer and use it in GitHub Desktop.
Free response #4
Do you like classical music? I like classical music. Anyway...let's
say we're going to see a concert with a bunch of friends. We want
to sit together in a row and there's 5 of us. Furthermore we like to
sit close to the front. Cause you know, better view and everything.
So given a seating chart, I want you to write a method that will
tell us the best row where we can sit.
The seating chart is going to be a 2D array like so...
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]
[0 1 0 1 0 1 1 1 0 0 1 1 0 0 0 0 1]
[0 0 0 1 1 1 0 0 1 1 1 1 1 1 1 1 0]
[0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
[1 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1]
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
...where the end of the 2D array (i.e. bottom rows) correspond
to seats closer to the front of the concert hall and the start
of the array corresponds to seats near the end. A 1 means the seat
is already taken. A 0 means that the seat is empty.
So, as you can see (hopefully) the seating question boils down to
finding the lowest row with 5 zeros in-a-row.
/* Returns the closest row to the front where "numPeople" of
* people can sit together side-by-side. If there is no such
* row (meaning seats are occupied in a way such that you can't
* find "numPeople" empty spots together), then just return -1. */
public int findRowToSitIn(int[][] seats, int numPeople) {
/* TODO */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment