+ 3

Where is misstake?

Help me. This cod on JS; https://code.sololearn.com/W1y24yZKZ820 Why 75+64+82=217? Task: https://projecteuler.net/problem=18

25th Oct 2017, 6:07 AM
Константин Дудченко
Константин Дудченко - avatar
15 Answers
+ 13
not exactly you also have to consider that it is a path so the max elements from each row need to be adjacent need to also consider the index of the previous max element and the index difference in the next row
25th Oct 2017, 7:25 AM
Burey
Burey - avatar
+ 11
something is wrong in your algorithm it can also be solved with a much shorter approach function MPS(pyramid){ var sum = 0; for(var i=0;i<pyramid.length;i++){ var row = pyramid[i]; sum+=Math.max.apply(null, row); } return sum; } iterate on each row of the pyramid, and add to the sum the max value of each row in a single command http://www.jstips.co/en/javascript/calculate-the-max-min-value-from-an-array/ can probably replace the for loop with a reduce function function MPS(pyramid){ return pyramid.reduce(function(sum, row){ return Math.max.apply(null, row) + parseInt(sum); }); } edit: my solution is wrong and does not solves the exercise correctly will try later on after work
25th Oct 2017, 6:35 AM
Burey
Burey - avatar
+ 10
i get 1313 for the big pyramid
25th Oct 2017, 6:54 AM
Burey
Burey - avatar
+ 10
my bad,misunderstood the exercise i'll try it later after work
25th Oct 2017, 7:04 AM
Burey
Burey - avatar
+ 10
can only move diagonally with a constraint of staying on a path (keep the difference of the index between each row to 1)
25th Oct 2017, 8:38 AM
Burey
Burey - avatar
+ 10
this solution runs from bottom to top and for each row check every two adjacent numbers and adds the bigger on to the row above it at the lower index eventually you will end up with the max sum at the top of the pyramid at pyramid[0][0] also loaded the pyramid from problem 67, parsed into an array and solved https://code.sololearn.com/WC113YR9qDAi/?ref=app
25th Oct 2017, 7:18 PM
Burey
Burey - avatar
+ 8
i think your code is ok for the little pyramid [75] [95, 64] [17, 47, 82] 75+95+47 (82 is too far away to reach) = 217
25th Oct 2017, 5:09 PM
Burey
Burey - avatar
+ 7
So like a path of numbers where you can move diagonally too, that returns the maximum sum?
25th Oct 2017, 7:29 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
see task more. There no max in row. Sory for my English console.log(MPS([ [75], [95, 64], [17, 47, 82], [18, 35, 87, 10], [20, 4, 82, 47, 65], [19, 1, 23, 75, 3, 34], [88, 2, 77, 73, 7, 63, 67], [99, 65, 4, 28, 6, 16, 70, 92], [41, 41, 26, 56, 83, 40, 80, 70, 33], [41, 48, 72, 33, 47, 32, 37, 16, 94, 29], [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14], [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57], [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48], [63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31], [4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23] ])); must be 1074
25th Oct 2017, 6:50 AM
Константин Дудченко
Константин Дудченко - avatar
+ 5
see: console.log(MPS([ [3], [7, 4], [2, 4, 6], [8, 5, 9, 3] ])); must 3+7+4+9=23, in you code answer 25.
25th Oct 2017, 6:56 AM
Константин Дудченко
Константин Дудченко - avatar
+ 4
Wait so what is the problen here? I don't get it
25th Oct 2017, 7:00 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
must be max sum element in column, but not max sum element in row
25th Oct 2017, 7:02 AM
Константин Дудченко
Константин Дудченко - avatar
+ 4
So sum of maximum elements of inputted arrays?
25th Oct 2017, 7:09 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
May see my code, why summ is not right? My algorithm is Ok, his summ right column, but summ - not right
25th Oct 2017, 4:55 PM
Константин Дудченко
Константин Дудченко - avatar
+ 2
my algorithm not check last column, May be and others column, when they a lot of. how it do??? algorithm not right
25th Oct 2017, 5:52 PM
Константин Дудченко
Константин Дудченко - avatar