Skip to content
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
35 changes: 35 additions & 0 deletions lcp/LCP 22. 黑白方格画/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,41 @@ function paintingPlan(n: number, k: number): number {
}
```

#### Swift

```swift
class Solution {
func paintingPlan(_ n: Int, _ k: Int) -> Int {
if k == 0 || k == n * n {
return 1
}

func combination(_ n: Int, _ r: Int) -> Int {
guard r <= n else { return 0 }
if r == 0 || r == n { return 1 }
var result = 1
for i in 0..<r {
result = result * (n - i) / (i + 1)
}
return result
}

var ans = 0

for i in 0...n {
for j in 0...n {
let paintedCells = n * (i + j) - i * j
if paintedCells == k {
ans += combination(n, i) * combination(n, j)
}
}
}

return ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
30 changes: 30 additions & 0 deletions lcp/LCP 22. 黑白方格画/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
func paintingPlan(_ n: Int, _ k: Int) -> Int {
if k == 0 || k == n * n {
return 1
}

func combination(_ n: Int, _ r: Int) -> Int {
guard r <= n else { return 0 }
if r == 0 || r == n { return 1 }
var result = 1
for i in 0..<r {
result = result * (n - i) / (i + 1)
}
return result
}

var ans = 0

for i in 0...n {
for j in 0...n {
let paintedCells = n * (i + j) - i * j
if paintedCells == k {
ans += combination(n, i) * combination(n, j)
}
}
}

return ans
}
}
Loading