Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add swift implementation to lcp problem: No.56 #3931

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions lcp/LCP 56. 信物传送/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Expand Down
46 changes: 46 additions & 0 deletions lcp/LCP 56. 信物传送/Solution.swift
Original file line number Diff line number Diff line change
@@ -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
}
}