Help on solving an algorithm

Let's talk about algorithms!

Moderator: Board moderators

Post Reply
azizul_hakim
New poster
Posts: 1
Joined: Mon Sep 30, 2013 1:47 am

Help on solving an algorithm

Post by azizul_hakim » Mon Sep 30, 2013 1:50 am

A patient has n pills to take. Each day he can take either 1 pill or 2 pills until all pills are gone. What will be the total number of different ways of taking n pills?

My approach as follows:

Each day take only one pill - 1 way

Take 2 pill only a single day, other days take only 1 pill - (n-1)C1 ways

Take 2 pill only 2 days, other days take 1 pill - (n-2)C2 ways

...

...

...

Take 2 pill on (n/2) days - (n/2)C(n/2) ways.

Is my algorithm correct? If yes, how can I find its closed form? If not, what might be the correct way?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Help on solving an algorithm

Post by brianfry713 » Tue Oct 01, 2013 1:13 am

Check input and AC output for thousands of problems on uDebug!

marlo
New poster
Posts: 4
Joined: Sat Sep 28, 2013 9:38 am
Location: USA
Contact:

Re: Help on solving an algorithm

Post by marlo » Tue Oct 01, 2013 9:53 am

Go to "answer.com". :D :D
“The best gifts come from the heart, not the store.”

xomla
New poster
Posts: 4
Joined: Fri Oct 11, 2013 7:15 am

Re: Help on solving an algorithm

Post by xomla » Sat Oct 12, 2013 7:39 am

D[1] = 1 (He takes the pill the next day.)
D[2] = 2 (1 + 1 = 2, 2 = 2)
D[3] = 3 (1 + 1 + 1 = 3, 1 + 2 = 3, 2 + 1 = 3) (D[k-1] + D[k - 2])
D[N] = D[N - 1] + D[N - 2]
It's a fibonacci sequence.

sara736
New poster
Posts: 1
Joined: Wed Sep 24, 2014 12:44 pm

Re: Help on solving an algorithm

Post by sara736 » Wed Sep 24, 2014 12:45 pm

If there is no unique solution, any of the possible solutions!
We offer pass4sure success for wikipedia exam with help of latest security+ certification and Rockefeller University practice questions and the exams of ARM ccie security PMI

Post Reply

Return to “Algorithms”