forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPascalTriangle.kt
61 lines (53 loc) · 1.73 KB
/
PascalTriangle.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
* Given an integer numRows, return the first numRows of Pascal's triangle.
* In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
*
* <img src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" height="150" width="150"/>
* [Source](https://leetcode.com/problems/pascals-triangle/)
*/
@UseCommentAsDocumentation
private fun pascalTriangle(numRows: Int): List<List<Int>> {
if (numRows == 1) {
return listOf(listOf(1))
}
val result = mutableListOf<List<Int>>()
result.add(listOf(1))
generateTriangle(1, maxRows = numRows, previous = listOf(1), result = result)
return result
}
private fun generateTriangle(current: Int, maxRows: Int, previous: List<Int>, result: MutableList<List<Int>>) {
if (current == maxRows) {
return
}
val currentRow = MutableList(current + 1) { 1 }
for (i in 1..current) { // 0 and lastIndex is always 1
// sum of the two numbers directly above it
currentRow[i] = (previous.getOrNull(i - 1) ?: 0) + (previous.getOrNull(i) ?: 0)
}
result.add(currentRow)
generateTriangle(current + 1, maxRows, currentRow, result)
}
fun main() {
pascalTriangle(numRows = 5) shouldBe listOf(
listOf(1),
listOf(1, 1),
listOf(1, 2, 1),
listOf(1, 3, 3, 1),
listOf(1, 4, 6, 4, 1)
)
pascalTriangle(numRows = 1) shouldBe listOf(
listOf(1),
)
pascalTriangle(numRows = 2) shouldBe listOf(
listOf(1),
listOf(1, 1),
)
pascalTriangle(numRows = 3) shouldBe listOf(
listOf(1),
listOf(1, 1),
listOf(1, 2, 1),
)
}