diff --git "a/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/README.md" "b/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/README.md" index 570853993406f..da2ee732ca727 100644 --- "a/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/README.md" +++ "b/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/README.md" @@ -178,6 +178,50 @@ func minJump(jump []int) int { } ``` +#### Swift + +```swift +class Solution { + func minJump(_ jump: [Int]) -> Int { + let n = jump.count + var vis = Array(repeating: false, count: n) + var queue = [0] + vis[0] = true + var ans = 0 + var maxReach = 1 + + while !queue.isEmpty { + ans += 1 + let size = queue.count + + for _ in 0..= n { + return ans + } + + if !vis[forwardJump] { + queue.append(forwardJump) + vis[forwardJump] = true + } + + while maxReach < i { + if !vis[maxReach] { + queue.append(maxReach) + vis[maxReach] = true + } + maxReach += 1 + } + } + } + + return -1 + } +} +``` + diff --git "a/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/Solution.swift" "b/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/Solution.swift" new file mode 100644 index 0000000000000..cb4693fc17269 --- /dev/null +++ "b/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/Solution.swift" @@ -0,0 +1,39 @@ +class Solution { + func minJump(_ jump: [Int]) -> Int { + let n = jump.count + var vis = Array(repeating: false, count: n) + var queue = [0] + vis[0] = true + var ans = 0 + var maxReach = 1 + + while !queue.isEmpty { + ans += 1 + let size = queue.count + + for _ in 0..= n { + return ans + } + + if !vis[forwardJump] { + queue.append(forwardJump) + vis[forwardJump] = true + } + + while maxReach < i { + if !vis[maxReach] { + queue.append(maxReach) + vis[maxReach] = true + } + maxReach += 1 + } + } + } + + return -1 + } +} \ No newline at end of file