From e8b3d679818f69cd4bac75ae4bb578bd6343fc65 Mon Sep 17 00:00:00 2001 From: VicRen Date: Sat, 17 Oct 2020 21:48:29 +0800 Subject: [PATCH] 18: maximum path sum I --- 18_maximum_path_sum_I/digits.txt | 15 +++++ 18_maximum_path_sum_I/main.go | 64 +++++++++++++++++++ .../maximum_path_sum_test.go | 43 +++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 18_maximum_path_sum_I/digits.txt create mode 100644 18_maximum_path_sum_I/main.go create mode 100644 18_maximum_path_sum_I/maximum_path_sum_test.go diff --git a/18_maximum_path_sum_I/digits.txt b/18_maximum_path_sum_I/digits.txt new file mode 100644 index 0000000..bc0ed83 --- /dev/null +++ b/18_maximum_path_sum_I/digits.txt @@ -0,0 +1,15 @@ +75 +95 64 +17 47 82 +18 35 87 10 +20 04 82 47 65 +19 01 23 75 03 34 +88 02 77 73 07 63 67 +99 65 04 28 06 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 04 68 89 53 67 30 73 16 69 87 40 31 +04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 \ No newline at end of file diff --git a/18_maximum_path_sum_I/main.go b/18_maximum_path_sum_I/main.go new file mode 100644 index 0000000..de233d0 --- /dev/null +++ b/18_maximum_path_sum_I/main.go @@ -0,0 +1,64 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "os" + "strconv" + "strings" +) + +func main() { + fi, err := os.Open("./digits.txt") + if err != nil { + panic(err) + } + defer fi.Close() + + br := bufio.NewReader(fi) + var input [][]int + for { + a, _, e := br.ReadLine() + if e == io.EOF { + break + } + got := strings.Split(string(a), " ") + var line []int + for _, v := range got { + n, err := strconv.Atoi(v) + if err != nil { + panic(err) + } + line = append(line, n) + } + input = append(input, line) + } + fmt.Println("input=", input) + + fmt.Println("maximum path sum is:", LargestSumOfTriangle(input)) +} + +func LargestSumOfTriangle(input [][]int) int { + rest := input + l := len(input) + for l > 1 { + hl := l - 2 + var line []int + for n, v := range input[l-2] { + x := v + max(input[l-1][n], input[l-1][n+1]) + line = append(line, x) + } + rest = rest[:hl] + rest = append(rest, line) + l = len(rest) + } + return rest[0][0] +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} diff --git a/18_maximum_path_sum_I/maximum_path_sum_test.go b/18_maximum_path_sum_I/maximum_path_sum_test.go new file mode 100644 index 0000000..53095b1 --- /dev/null +++ b/18_maximum_path_sum_I/maximum_path_sum_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "testing" +) + +func TestLargestSumOfTriangle(t *testing.T) { + tt := []struct { + name string + input [][]int + want int + }{ + { + "example", + [][]int{{3}, {7, 4}, {2, 4, 6}, {8, 5, 9, 3}}, + 23, + }, + { + "example_2", + [][]int{{3}, {7, 4}}, + 10, + }, + { + "example_3", + [][]int{{3}}, + 3, + }, + { + "example_4", + [][]int{{3}, {7, 4}, {2, 4, 6}}, + 14, + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + got := LargestSumOfTriangle(tc.input) + if got != tc.want { + t.Errorf("LargestSumOfTriangle(%v)=%d, want %d", tc.input, got, tc.want) + } + }) + } +}