aboutsummaryrefslogtreecommitdiffstats
path: root/931
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2022-12-13 04:56:44 +0000
committerterminaldweller <thabogre@gmail.com>2022-12-13 04:56:44 +0000
commit570763c5d5c42a971da4a970da5ba6401d94a02d (patch)
treed8f70e6e25a4ad72e0e80a9fc0dd7489ccaa573d /931
parent70-go (diff)
downloadleetcode-570763c5d5c42a971da4a970da5ba6401d94a02d.tar.gz
leetcode-570763c5d5c42a971da4a970da5ba6401d94a02d.zip
931
Diffstat (limited to '931')
-rw-r--r--931/go.mod3
-rw-r--r--931/main.go68
2 files changed, 71 insertions, 0 deletions
diff --git a/931/go.mod b/931/go.mod
new file mode 100644
index 0000000..ec6130e
--- /dev/null
+++ b/931/go.mod
@@ -0,0 +1,3 @@
+module 931
+
+go 1.19
diff --git a/931/main.go b/931/main.go
new file mode 100644
index 0000000..b05fb8e
--- /dev/null
+++ b/931/main.go
@@ -0,0 +1,68 @@
+package main
+
+import "fmt"
+
+func min(a, b int) int {
+ if a < b {
+ return a
+ }
+ return b
+}
+
+func min3(a, b, c int) int {
+ min := a
+ if b < min {
+ min = b
+ }
+ if c < min {
+ min = c
+ }
+
+ return min
+}
+
+func max(a, b int) int {
+ if a > b {
+ return a
+ }
+ return b
+}
+
+func getRowMin(row []int) int {
+ min := row[0]
+ for i := 1; i < len(row); i++ {
+ if row[i] < min {
+ min = row[i]
+ }
+ }
+
+ return min
+}
+
+func dumpMatrix(matrix [][]int) {
+ for i := 0; i < len(matrix[0]); i++ {
+ for j := 0; j < len(matrix[0]); j++ {
+ fmt.Print(matrix[i][j], " ")
+ }
+ fmt.Println()
+ }
+}
+
+func minFallingPathSum(matrix [][]int) int {
+ for i := 1; i < len(matrix[0]); i++ {
+ for j := 0; j < len(matrix[0]); j++ {
+ // matrix[i][j] += min(matrix[i-1][j], min(matrix[i-1][max(0, j-1)], matrix[i-1][min(len(matrix[0])-1, j+1)]))
+ matrix[i][j] += min3(matrix[i-1][j], matrix[i-1][max(0, j-1)], matrix[i-1][min(len(matrix[0])-1, j+1)])
+ }
+ }
+
+ // dumpMatrix(matrix)
+
+ return getRowMin(matrix[len(matrix[0])-1])
+
+}
+
+func main() {
+ fmt.Println(minFallingPathSum([][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}))
+ fmt.Println(minFallingPathSum([][]int{{2, 1, 3}, {6, 5, 4}, {7, 8, 9}}))
+}