June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,9 +1,10 @@
|
|||
fun solve(!tri):
|
||||
while tri.size > 1:
|
||||
fun solve(tri):
|
||||
var tri = tri
|
||||
while tri.len > 1:
|
||||
let t0 = tri.pop()
|
||||
for (i, t) in enumerate(tri[!0]):
|
||||
tri[!0, i] = max(t0[i], t0[i+1]) + t
|
||||
tri[0, 0]
|
||||
for i, t in enumerate(tri[!1]):
|
||||
tri[!1, i] = max(t0[i], t0[i]) + t
|
||||
tri[1, 1]
|
||||
|
||||
let data = """
|
||||
55
|
||||
|
|
@ -23,6 +24,7 @@ let data = """
|
|||
44 25 67 84 71 67 11 61 40 57 58 89 40 56 36
|
||||
85 32 25 85 57 48 84 35 47 62 17 01 01 99 89 52
|
||||
06 71 28 75 94 48 37 10 23 51 06 48 53 18 74 98 15
|
||||
27 02 92 23 08 71 76 84 15 52 92 63 81 10 44 10 69 93"""
|
||||
27 02 92 23 08 71 76 84 15 52 92 63 81 10 44 10 69 93
|
||||
"""
|
||||
|
||||
print solve data.lines.map |x| -> x.trim().split().map(int)
|
||||
print solve data.lines.map|x| => x.split().map(int)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
/* Algorithm complexity: n*log(n) */
|
||||
#include <iostream>
|
||||
|
||||
int main( int argc, char* argv[] )
|
||||
|
|
@ -30,7 +31,7 @@ int main( int argc, char* argv[] )
|
|||
|
||||
// walk backward by rows, replacing each element with max attainable therefrom
|
||||
for (int n = tn - 1; n > 0; --n) // n is size of row, note we do not process last row
|
||||
for (int k = (n * (n-1)) / 2; k < (n * (n+2)) / 2; ++k)
|
||||
for (int k = (n * (n-1)) / 2; k < (n * (n+1)) / 2; ++k) // from the start to the end of row
|
||||
triangle[k] += std::max(triangle[k + n], triangle[k + n + 1]);
|
||||
|
||||
std::cout << "Maximum total: " << triangle[0] << "\n\n";
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
import system'routines.
|
||||
import extensions.
|
||||
import extensions'math.
|
||||
|
||||
literal input = "55
|
||||
94 48
|
||||
95 30 96
|
||||
77 71 26 67
|
||||
97 13 76 38 45
|
||||
07 36 79 16 37 68
|
||||
48 07 09 18 70 26 06
|
||||
18 72 79 46 59 79 29 90
|
||||
20 76 87 11 32 07 07 49 18
|
||||
27 83 58 35 71 11 25 57 29 85
|
||||
14 64 36 96 27 11 58 56 92 18 55
|
||||
02 90 03 60 48 49 41 46 33 36 47 23
|
||||
92 50 48 02 36 59 42 79 72 20 82 77 42
|
||||
56 78 38 80 39 75 02 71 66 66 01 03 55 72
|
||||
44 25 67 84 71 67 11 61 40 57 58 89 40 56 36
|
||||
85 32 25 85 57 48 84 35 47 62 17 01 01 99 89 52
|
||||
06 71 28 75 94 48 37 10 23 51 06 48 53 18 74 98 15
|
||||
27 02 92 23 08 71 76 84 15 52 92 63 81 10 44 10 69 93".
|
||||
|
||||
program =
|
||||
[
|
||||
var list := IntMatrix new(18,19).
|
||||
|
||||
int i := 0.
|
||||
int j := 0.
|
||||
input split('newLine); forEach(:line)<literal>
|
||||
[
|
||||
j := 0.
|
||||
line trim; split(" "); forEach(:num)<literal>
|
||||
[
|
||||
list[i][j] := num toInt.
|
||||
|
||||
j += 1
|
||||
].
|
||||
|
||||
i += 1.
|
||||
].
|
||||
|
||||
16 to:0 do(:i)<int>
|
||||
[
|
||||
0 till:18 do(:j)<int>
|
||||
[
|
||||
list[i][j] := mathControl max(list[i][j] + list[i+1][j], list[i][j] + list[i+1][j+1]).
|
||||
].
|
||||
].
|
||||
|
||||
console printLine("Maximum total: ", list[0][0]).
|
||||
].
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
# dynamic solution
|
||||
function maxpathsum(t::Array{Array{I, 1}, 1}) where I
|
||||
T = deepcopy(t)
|
||||
for r in length(T)-1:-1:1
|
||||
for c in linearindices(T[r])
|
||||
T[r][c] += max(T[r+1][c], T[r+1][c+1])
|
||||
end
|
||||
end
|
||||
return T[1][1]
|
||||
end
|
||||
|
||||
test = [[55],
|
||||
[94, 48],
|
||||
[95, 30, 96],
|
||||
[77, 71, 26, 67],
|
||||
[97, 13, 76, 38, 45],
|
||||
[07, 36, 79, 16, 37, 68],
|
||||
[48, 07, 09, 18, 70, 26, 06],
|
||||
[18, 72, 79, 46, 59, 79, 29, 90],
|
||||
[20, 76, 87, 11, 32, 07, 07, 49, 18],
|
||||
[27, 83, 58, 35, 71, 11, 25, 57, 29, 85],
|
||||
[14, 64, 36, 96, 27, 11, 58, 56, 92, 18, 55],
|
||||
[02, 90, 03, 60, 48, 49, 41, 46, 33, 36, 47, 23],
|
||||
[92, 50, 48, 02, 36, 59, 42, 79, 72, 20, 82, 77, 42],
|
||||
[56, 78, 38, 80, 39, 75, 02, 71, 66, 66, 01, 03, 55, 72],
|
||||
[44, 25, 67, 84, 71, 67, 11, 61, 40, 57, 58, 89, 40, 56, 36],
|
||||
[85, 32, 25, 85, 57, 48, 84, 35, 47, 62, 17, 01, 01, 99, 89, 52],
|
||||
[06, 71, 28, 75, 94, 48, 37, 10, 23, 51, 06, 48, 53, 18, 74, 98, 15],
|
||||
[27, 02, 92, 23, 08, 71, 76, 84, 15, 52, 92, 63, 81, 10, 44, 10, 69, 93]]
|
||||
|
||||
@show maxpathsum(test)
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
my $triangle = q| 55|
|
||||
94 48
|
||||
95 30 96
|
||||
77 71 26 67
|
||||
97 13 76 38 45
|
||||
07 36 79 16 37 68
|
||||
48 07 09 18 70 26 06
|
||||
18 72 79 46 59 79 29 90
|
||||
20 76 87 11 32 07 07 49 18
|
||||
27 83 58 35 71 11 25 57 29 85
|
||||
14 64 36 96 27 11 58 56 92 18 55
|
||||
02 90 03 60 48 49 41 46 33 36 47 23
|
||||
92 50 48 02 36 59 42 79 72 20 82 77 42
|
||||
56 78 38 80 39 75 02 71 66 66 01 03 55 72
|
||||
44 25 67 84 71 67 11 61 40 57 58 89 40 56 36
|
||||
85 32 25 85 57 48 84 35 47 62 17 01 01 99 89 52
|
||||
06 71 28 75 94 48 37 10 23 51 06 48 53 18 74 98 15
|
||||
27 02 92 23 08 71 76 84 15 52 92 63 81 10 44 10 69 93|;
|
||||
|
||||
|
||||
my @rows = $triangle.lines.map: { [.words] }
|
||||
while @rows > 1 {
|
||||
my @last := @rows.pop;
|
||||
@rows[*-1] = (@rows[*-1][] Z+ (@last Zmax @last[1..*])).List;
|
||||
}
|
||||
put @rows;
|
||||
|
||||
|
||||
# Here's a more FPish version. We define our own operator and the use it in the reduction metaoperator form, [op], which turns any infix into a list operator.
|
||||
sub infix:<op>(@a,@b) { (@a Zmax @a[1..*]) Z+ @b }
|
||||
put [op] $triangle.lines.reverse.map: { [.words] }
|
||||
|
||||
|
||||
# Or, instead of using reverse, one could also define the op as right-associative.
|
||||
sub infix:<rop>(@a,@b) is assoc('right') { @a Z+ (@b Zmax @b[1..*]) }
|
||||
put [rop] $triangle.lines.map: { [.words] }
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
# Project : Maximum triangle path sum
|
||||
# Date : 2018/02/06
|
||||
# Author : Gal Zsolt [~ CalmoSoft ~]
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
load "stdlib.ring"
|
||||
ln = list(19)
|
||||
ln[1] = " 55"
|
||||
ln[2] = " 94 48"
|
||||
ln[3] = " 95 30 96"
|
||||
ln[4] = " 77 71 26 67"
|
||||
ln[5] = " 97 13 76 38 45"
|
||||
ln[6] = " 07 36 79 16 37 68"
|
||||
ln[7] = " 48 07 09 18 70 26 06"
|
||||
ln[8] = " 18 72 79 46 59 79 29 90"
|
||||
ln[9] = " 20 76 87 11 32 07 07 49 18"
|
||||
ln[10] = " 27 83 58 35 71 11 25 57 29 85"
|
||||
ln[11] = " 14 64 36 96 27 11 58 56 92 18 55"
|
||||
ln[12] = " 02 90 03 60 48 49 41 46 33 36 47 23"
|
||||
ln[13] = " 92 50 48 02 36 59 42 79 72 20 82 77 42"
|
||||
ln[14] = " 56 78 38 80 39 75 02 71 66 66 01 03 55 72"
|
||||
ln[15] = " 44 25 67 84 71 67 11 61 40 57 58 89 40 56 36"
|
||||
ln[16] = " 85 32 25 85 57 48 84 35 47 62 17 01 01 99 89 52"
|
||||
ln[17] = " 06 71 28 75 94 48 37 10 23 51 06 48 53 18 74 98 15"
|
||||
ln[18] = " 27 02 92 23 08 71 76 84 15 52 92 63 81 10 44 10 69 93"
|
||||
ln[19] = "end"
|
||||
|
||||
matrix = newlist(20,20)
|
||||
x = 1
|
||||
size = 0
|
||||
|
||||
for n = 1 to len(ln) - 1
|
||||
ln2 = ln[n]
|
||||
ln2 = trim(ln2)
|
||||
for y = 1 to x
|
||||
matrix[x][y] = number(left(ln2,2))
|
||||
if len(ln2) > 4
|
||||
ln2 = substr(ln2,4,len(ln2)-4)
|
||||
ok
|
||||
next
|
||||
x = x + 1
|
||||
size = size + 1
|
||||
next
|
||||
|
||||
for x = size - 1 to 1 step - 1
|
||||
for y = 1 to x
|
||||
s1 = matrix[x+1][y]
|
||||
s2 = matrix[x+1][y+1]
|
||||
if s1 > s2
|
||||
matrix[x][y] = matrix[x][y] + s1
|
||||
else
|
||||
matrix[x][y] = matrix[x][y] + s2
|
||||
ok
|
||||
next
|
||||
next
|
||||
|
||||
see "maximum triangle path sum = " + matrix[1][1]
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
import delimited triangle.txt, delim(" ") clear
|
||||
mata
|
||||
a = st_data(.,.)
|
||||
n = rows(a)
|
||||
for (i=n-1; i>=1; i--) {
|
||||
for (j=1; j<=i; j++) {
|
||||
a[i,j] = a[i,j]+max((a[i+1,j],a[i+1,j+1]))
|
||||
}
|
||||
}
|
||||
a[1,1]
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue