Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sorry but you're using an incorrect algorithm.<br> What you're using is called a <a href="http://en.wikipedia.org/wiki/Greedy_algorithm" rel="noreferrer">Greedy Algorithm</a>, but the correct algorithm to use is <a href="http://en.wikipedia.org/wiki/Dynamic_programming" rel="noreferrer">Dynamic Programming</a>. The major difference is that greedy choose a best current option as a choice, while dynamic programming enumerates EVERY possible option and generate a series of choices. </p> <p>There is a simple case on which your solution(greedy) would fail:</p> <pre><code>0, 1, 0, 0, 0, 10 </code></pre> <p>The best result is 10, but your algorithm would give 1 instead.</p> <p>Think about it yourself for a moment, and then try seeking for information on Dynamic Programming. Project Euler is a great place and it's feels utterly great when you come up with a solution. So I won't say many on it for now. :)</p> <p><strong>UPDATED:</strong> </p> <blockquote> <p>But Ques: By starting at the top of the triangle below and moving to "adjacent numbers" on the row below. I have made my code according to this Word. Sorry for interrupting but can you make me more clear with this word ? </p> </blockquote> <p>Notice there would be actually <code>2^(n-1)</code> possible routes on a given <code>n</code> level triangle. And in the original problem <code>maximum total</code> means the maximum total among all these routes.<br> There's no grantee that your code would find the maximum among <code>ALL</code> routes since you only choose a better one in <code>TWO</code> choices at any step.</p> <p><strong>AGAIN UPDATED:</strong></p> <p>Actually in this problem since <code>n=15</code> is small enough, you can also enumerate all <code>2^(n-1)=16384</code> possible routes, summarize the total value of each route, and finally get a maximum total among all you get. However in <a href="https://projecteuler.net/index.php?section=problems&amp;id=67" rel="noreferrer">Problem 67 of Project Euler</a> the problem size <code>n</code> is increased to 100 and it would be not possible to enumerate all <code>2^(n-1)=633825300114114700748351602688</code> routes. </p> <p>By the way, I've posted a link to wiki page of Dynamic Programming, but I'm afraid it's to complex to read as a starter. Sorry for that.<br> But don't worry, just Google <a href="https://www.google.com/search?q=dynamic+programming+tutorial" rel="noreferrer">dynamic programming tutorial</a> and you'll got many useful resources to see :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload