Please solve the programming task below using a self-contained code snippet in a markdown code block.

You are painting a fence of n posts with k different colors. You must paint the posts following these rules:

Every post must be painted exactly one color.
There cannot be three or more consecutive posts with the same color.

Given the two integers n and k, return the number of ways you can paint the fence.
 
Please complete the following python code precisely:
```python
class Solution:
    def numWays(self, n: int, k: int) -> int:
```