Showing posts with label algorithms. Show all posts
Showing posts with label algorithms. Show all posts

Sunday, October 24, 2010

Bridge Crossing

There are four people who want to cross a bridge. They take 1, 2, 5 and 10 minutes, respectively. They're crossing at night, so, to cross, you need a lantern, and they have one between the four of them. The bridge is only wide enough to fit two people crossing at a time.

What's the floor on their crossing time? That is, what's the shortest possible time for them all to cross?

Friday, October 22, 2010

A Wonderful Puzzle

(thanks to Wendy for giving this to me)

You read in a stream of integers one by one. You don't know in advance how long the sequence will be, though, of course, you can recognize the EOF char when you get to it. You have to find which integer is the majority, where the majority is defined to be the integer that appears at least (n/2) + 1 times, where n is the length of the sequence.

Do it in constant memory. And, linear time, of course.