From 747bf7a810857c8da4c3aa73f9775d62de0a50ca Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Wed, 30 Oct 2024 08:35:05 +0100 Subject: [PATCH] feat: add swift implementation to lcof2 problem: No.112 --- .../README.md" | 43 +++++++++++++++++++ .../Solution.swift" | 38 ++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/Solution.swift" diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/README.md" index 4e8337208369c..a30f5055009ad 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/README.md" @@ -205,6 +205,49 @@ func longestIncreasingPath(matrix [][]int) int { } ``` +#### Swift + +```swift +class Solution { + private var memo: [[Int]] = [] + private var matrix: [[Int]] = [] + private var m: Int = 0 + private var n: Int = 0 + + func longestIncreasingPath(_ matrix: [[Int]]) -> Int { + self.matrix = matrix + m = matrix.count + n = matrix[0].count + memo = Array(repeating: Array(repeating: -1, count: n), count: m) + + var ans = 0 + for i in 0.. Int { + if memo[i][j] != -1 { + return memo[i][j] + } + var ans = 1 + let dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)] + + for (dx, dy) in dirs { + let x = i + dx, y = j + dy + if x >= 0, x < m, y >= 0, y < n, matrix[x][y] > matrix[i][j] { + ans = max(ans, dfs(x, y) + 1) + } + } + memo[i][j] = ans + return ans + } +} +``` + diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/Solution.swift" new file mode 100644 index 0000000000000..a1bca0d1e6a02 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/Solution.swift" @@ -0,0 +1,38 @@ +class Solution { + private var memo: [[Int]] = [] + private var matrix: [[Int]] = [] + private var m: Int = 0 + private var n: Int = 0 + + func longestIncreasingPath(_ matrix: [[Int]]) -> Int { + self.matrix = matrix + m = matrix.count + n = matrix[0].count + memo = Array(repeating: Array(repeating: -1, count: n), count: m) + + var ans = 0 + for i in 0.. Int { + if memo[i][j] != -1 { + return memo[i][j] + } + var ans = 1 + let dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)] + + for (dx, dy) in dirs { + let x = i + dx, y = j + dy + if x >= 0, x < m, y >= 0, y < n, matrix[x][y] > matrix[i][j] { + ans = max(ans, dfs(x, y) + 1) + } + } + memo[i][j] = ans + return ans + } +}