From b22263c216181d0b20f604f09885246309adfc51 Mon Sep 17 00:00:00 2001 From: Lanre Adedara <lanre.adedara@shara.co> Date: Mon, 6 Jan 2025 07:48:14 +0100 Subject: [PATCH 1/2] feat: add swift implementation to lcp problem: No.56 --- .../README.md" | 51 +++++++++++++++++++ .../Solution.swift" | 46 +++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 "lcp/LCP 56. \344\277\241\347\211\251\344\274\240\351\200\201/Solution.swift" diff --git "a/lcp/LCP 56. \344\277\241\347\211\251\344\274\240\351\200\201/README.md" "b/lcp/LCP 56. \344\277\241\347\211\251\344\274\240\351\200\201/README.md" index b66d379ba9b7d..ad36d001d7b5e 100644 --- "a/lcp/LCP 56. \344\277\241\347\211\251\344\274\240\351\200\201/README.md" +++ "b/lcp/LCP 56. \344\277\241\347\211\251\344\274\240\351\200\201/README.md" @@ -271,6 +271,57 @@ function conveyorBelt(matrix: string[], start: number[], end: number[]): number } ``` +#### Swift + +```swift +class Solution { + func conveyorBelt(_ matrix: [String], _ start: [Int], _ end: [Int]) -> Int { + let directions: [(Int, Int)] = [(-1, 0), (0, 1), (1, 0), (0, -1)] + let directionMap: [Character: Int] = ["^": 0, ">": 1, "v": 2, "<": 3] + + let rows = matrix.count + let cols = matrix[0].count + + var dist = Array(repeating: Array(repeating: Int.max, count: cols), count: rows) + var deque: [(Int, Int)] = [] + + dist[start[0]][start[1]] = 0 + deque.append((start[0], start[1])) + + while !deque.isEmpty { + let (i, j) = deque.removeFirst() + + if i == end[0] && j == end[1] { + return dist[i][j] + } + + for (k, (di, dj)) in directions.enumerated() { + let ni = i + di + let nj = j + dj + + if ni >= 0 && ni < rows && nj >= 0 && nj < cols { + let currentChar = matrix[i][matrix[i].index(matrix[i].startIndex, offsetBy: j)] + let additionalCost = directionMap[currentChar] == k ? 0 : 1 + let newDist = dist[i][j] + additionalCost + + if newDist < dist[ni][nj] { + dist[ni][nj] = newDist + + if additionalCost == 0 { + deque.insert((ni, nj), at: 0) + } else { + deque.append((ni, nj)) + } + } + } + } + } + + return -1 + } +} +``` + <!-- tabs:end --> <!-- solution:end --> diff --git "a/lcp/LCP 56. \344\277\241\347\211\251\344\274\240\351\200\201/Solution.swift" "b/lcp/LCP 56. \344\277\241\347\211\251\344\274\240\351\200\201/Solution.swift" new file mode 100644 index 0000000000000..49e38bff757e2 --- /dev/null +++ "b/lcp/LCP 56. \344\277\241\347\211\251\344\274\240\351\200\201/Solution.swift" @@ -0,0 +1,46 @@ +class Solution { + func conveyorBelt(_ matrix: [String], _ start: [Int], _ end: [Int]) -> Int { + let directions: [(Int, Int)] = [(-1, 0), (0, 1), (1, 0), (0, -1)] + let directionMap: [Character: Int] = ["^": 0, ">": 1, "v": 2, "<": 3] + + let rows = matrix.count + let cols = matrix[0].count + + var dist = Array(repeating: Array(repeating: Int.max, count: cols), count: rows) + var deque: [(Int, Int)] = [] + + dist[start[0]][start[1]] = 0 + deque.append((start[0], start[1])) + + while !deque.isEmpty { + let (i, j) = deque.removeFirst() + + if i == end[0] && j == end[1] { + return dist[i][j] + } + + for (k, (di, dj)) in directions.enumerated() { + let ni = i + di + let nj = j + dj + + if ni >= 0 && ni < rows && nj >= 0 && nj < cols { + let currentChar = matrix[i][matrix[i].index(matrix[i].startIndex, offsetBy: j)] + let additionalCost = directionMap[currentChar] == k ? 0 : 1 + let newDist = dist[i][j] + additionalCost + + if newDist < dist[ni][nj] { + dist[ni][nj] = newDist + + if additionalCost == 0 { + deque.insert((ni, nj), at: 0) + } else { + deque.append((ni, nj)) + } + } + } + } + } + + return -1 + } +} \ No newline at end of file From 0355beb7d99148a067a5da04a605d03e55b049bf Mon Sep 17 00:00:00 2001 From: klever34 <klever34@users.noreply.github.com> Date: Mon, 6 Jan 2025 06:52:41 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- .../README.md" | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git "a/lcp/LCP 56. \344\277\241\347\211\251\344\274\240\351\200\201/README.md" "b/lcp/LCP 56. \344\277\241\347\211\251\344\274\240\351\200\201/README.md" index ad36d001d7b5e..79d933a073abb 100644 --- "a/lcp/LCP 56. \344\277\241\347\211\251\344\274\240\351\200\201/README.md" +++ "b/lcp/LCP 56. \344\277\241\347\211\251\344\274\240\351\200\201/README.md" @@ -278,35 +278,35 @@ class Solution { func conveyorBelt(_ matrix: [String], _ start: [Int], _ end: [Int]) -> Int { let directions: [(Int, Int)] = [(-1, 0), (0, 1), (1, 0), (0, -1)] let directionMap: [Character: Int] = ["^": 0, ">": 1, "v": 2, "<": 3] - + let rows = matrix.count let cols = matrix[0].count - + var dist = Array(repeating: Array(repeating: Int.max, count: cols), count: rows) var deque: [(Int, Int)] = [] - + dist[start[0]][start[1]] = 0 deque.append((start[0], start[1])) - + while !deque.isEmpty { let (i, j) = deque.removeFirst() - + if i == end[0] && j == end[1] { return dist[i][j] } - + for (k, (di, dj)) in directions.enumerated() { let ni = i + di let nj = j + dj - + if ni >= 0 && ni < rows && nj >= 0 && nj < cols { let currentChar = matrix[i][matrix[i].index(matrix[i].startIndex, offsetBy: j)] let additionalCost = directionMap[currentChar] == k ? 0 : 1 let newDist = dist[i][j] + additionalCost - + if newDist < dist[ni][nj] { dist[ni][nj] = newDist - + if additionalCost == 0 { deque.insert((ni, nj), at: 0) } else { @@ -316,7 +316,7 @@ class Solution { } } } - + return -1 } }