Skip to content

[pull] main from doocs:main #205

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

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: add swift implementation to lcp problem: No.25 (doocs#3774)
  • Loading branch information
klever34 authored Nov 18, 2024
commit 038e542aebfd91b655fe8554dd0f55b9eed1fe4b
35 changes: 35 additions & 0 deletions lcp/LCP 25. 古董键盘/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,41 @@ function keyboard(k: number, n: number): number {
}
```

#### Swift

```swift
class Solution {
func keyboard(_ k: Int, _ n: Int) -> Int {
let mod = 1_000_000_007
var c = Array(repeating: Array(repeating: 0, count: k + 1), count: n + 1)
for i in 0...n {
c[i][0] = 1
}

for i in 1...n {
for j in 1...k {
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod
}
}

var f = Array(repeating: Array(repeating: 0, count: 27), count: n + 1)
for j in 0..<27 {
f[0][j] = 1
}

for i in 1...n {
for j in 1..<27 {
for h in 0...min(i, k) {
f[i][j] = (f[i][j] + (f[i - h][j - 1] * c[i][h]) % mod) % mod
}
}
}

return f[n][26]
}
}
```

<!--- tabs:end -->

<!-- solution:end -->
Expand Down
30 changes: 30 additions & 0 deletions lcp/LCP 25. 古董键盘/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
func keyboard(_ k: Int, _ n: Int) -> Int {
let mod = 1_000_000_007
var c = Array(repeating: Array(repeating: 0, count: k + 1), count: n + 1)
for i in 0...n {
c[i][0] = 1
}

for i in 1...n {
for j in 1...k {
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod
}
}

var f = Array(repeating: Array(repeating: 0, count: 27), count: n + 1)
for j in 0..<27 {
f[0][j] = 1
}

for i in 1...n {
for j in 1..<27 {
for h in 0...min(i, k) {
f[i][j] = (f[i][j] + (f[i - h][j - 1] * c[i][h]) % mod) % mod
}
}
}

return f[n][26]
}
}