25.03.2013 Views

Cracking the Coding Interview - Fooo

Cracking the Coding Interview - Fooo

Cracking the Coding Interview - Fooo

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Solutions to Chapter 6 | Brain Teasers<br />

» Condition one (P(1) is true) ignites this chain, with truth cascading off into infinity<br />

Base Case: c = 1 (See previous page).<br />

Assume true for c hats. i.e., if <strong>the</strong>re are c hats, it will take c nights to remove all of <strong>the</strong>m.<br />

Prove true for c+1 hats.<br />

Each man with a hat sees c hat, and can not be immediately sure whe<strong>the</strong>r <strong>the</strong>re are c hats or<br />

c+1 hats However, he knows that if <strong>the</strong>re are c hats, it will take exactly c nights to remove<br />

<strong>the</strong>m Therefore, when c nights have passed and everyone still has a hat, he can only conclude<br />

that <strong>the</strong>re are c+1 hats He must know that he is wearing a hat Each man makes <strong>the</strong><br />

same conclusion and simultaneously removes <strong>the</strong> hats on night c+1<br />

Therefore, we have met <strong>the</strong> principles of induction We have proven that it will take c nights<br />

to remove c hats<br />

1 4 7<br />

<strong>Cracking</strong> <strong>the</strong> <strong>Coding</strong> <strong>Interview</strong> | Concepts and Algorithms

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!