13
2
You are a young programming geek living with your 2 other best friends. Every week, one of you has to do all the chores of the house and you decide whose turn it is by picking a stick. The one who picks the shortest stick loses and does all the chores.
As all of you are programmers and love creating puzzles, you have modified the "Pick the shortest stick" into a computer puzzle.
Here are the rules of the puzzle.
- You will be given a 2D matrix, where each column represents a stick.
- In each column, 1 represents a portion of the stick and 0 is an empty space
- When going from top to bottom in each column, initially you have
0
's and as soon as you hit a1
, the stick has started and rest of the column will be filled with1
only - You can write your program to pick one column. The size of the stick in that column determines the winner/loser. Size of stick == number of 1s in that column.
- However, that program can only have a linear worst-case time complexity.
As all of you are programmers, you will know if someone else's program is shooting the time complexity limit.
Your job is to:
- Write a program or function that accepts input in either a 2D format or array of strings.
- Input can be taken from STDIN/prompt/console or a function argument.
- If you are reading the input from STDIN/prompt then you can assume that the reading of input and converting it to an array takes 0 time (even though the code to do so has to be there in your answer)
- Determine the column with the longest stick in it.
- Output can be the function's return value or to STDOUT/console/alert.
- The program/function must have linear worst-case time complexity,
O(m+n)
wherem
is the number of rows andn
the number of columns.
Input can be either one of the following formats:
2D array:
[ [0, 0, 0, 0],
[1, 0, 0, 0],
[1, 1, 0, 1],
[1, 1, 1, 1] ]
Array of Strings:
[ "0000", "1000", "1101", "1111" ]
The input will have following properties:
- Size of the array is unknown, assume a rectangle of any size
- In any column, coming top down, if you see a 1, then everything below will be a one
- Empty-columns (i.e. 0-length) sticks are allowed.
This is a code-golf so shortest code wins!*
Please explain your code, or give the ungolfed version (to verify time complexity) along with which of the two input formats you expect.
UPDATE Linear time complexity here means O(n+m) where n is column size and m is row size. (For those who were unclear)
UPDATE 2 This definitely can be done in linear time. And if you are posting an answer, feel free to delay posting the logic/algorithm by a couple of days for a fair fight :)
UPDATE 3 I'll go over all answers in couple of hours to validate time complexity and the program :)
2I'd argue that this can't be done in O(n+m), since each cell could contain the crucial value (i.e. the first "1" of the longest stick/column). So you need to look at each cell, which takes O(n*m). – Falko – 2014-09-10T09:32:33.930
Could there be empty columns? – Martin Ender – 2014-09-10T09:48:33.783
@Optimizer: Oh, I see. You're right. :) – Falko – 2014-09-10T09:50:53.133
@Martin - Yes. stick length is 0 for that column then – Optimizer – 2014-09-10T10:02:21.847
11It can't be done in O(n+m). Once the input has been converted into a format which allows random access then the remaining processing can be O(n+m), but you require writing a program, and in the worst case that the only
1
in the input is the very last cell then it's necessary to read the entire input. Even if a language's standard library fakes random access to stdin, under the scenes it's buffering it and so the time taken is Omega(n*m). – Peter Taylor – 2014-09-10T10:26:03.880You can ignore the complexity achieved by reading the input from the STDIN and converting it into an array. Or, simply make a function which accepts an array :) But other than that, read Update 2 :) – Optimizer – 2014-09-10T10:31:42.253
2If you want to allow people to "simply make a function which accepts an array", the question shouldn't state that they must write a program. And if you require solutions which are in O(N^0.5) where N is the size of the input, you shouldn't ask for linear time solutions. A linear time solution can read the entire input. – Peter Taylor – 2014-09-10T10:37:54.737
@Optimizer you should make it clearer that the columns can't have 1's and 0's in any combination, but that the 1's have to be continuous and start from the start of the array. it really isn't specified, and there's only one example. – proud haskeller – 2014-09-10T19:02:38.077
@proud haskeller - Done – Optimizer – 2014-09-10T19:06:58.610
Can I take a
MultiList<of int>
instead ofList<of List<of int>>
? – Οurous – 2014-09-11T13:18:27.517