This is an excellent approach, but there are two potential issues with the specific solution. In other words, the algorithm is great, but the implementation is not quite there yet. One issue is that the test harness is probably happily supplying an infinite stream of integers, or at least more of them than are specified in the first input. If time limit is exceeded, I would guess that the stream just keeps going.
The second issue is that there could be, and probably are, integers that show up multiple times in the input stream. They would be lost here. So modifying Buk's solution:
chomp( my $count = <> ); my @numbers; $numbers[scalar <>]++ while $count--; foreach my $number ( 0 .. $#numbers ) { next unless defined $numbers[$number]; print "$number\n" for 1 .. $numbers[$number]; }
If that exceeds the time limitation, I'm with BrowserUk; they're nuts; this is an O(n) solution. ;)
Dave
In reply to Re^2: Sorting challenge (Insertion sort)
by davido
in thread Sorting challenge
by PerlSufi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |