Skip to content

Instantly share code, notes, and snippets.

@dasider41
Created September 15, 2019 23:14
Show Gist options
  • Save dasider41/7f503ba900618c92b507b8c84f78fd78 to your computer and use it in GitHub Desktop.
Save dasider41/7f503ba900618c92b507b8c84f78fd78 to your computer and use it in GitHub Desktop.
<?php
/*
In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
*/
/**
* @param Integer $numRows
* @return Integer[][]
*/
function generate(int $numRows): array
{
$result = [];
for ($i = 0; $i < $numRows; $i++) {
$temp[0] = 1;
for ($j = 1; $j < $i; $j++) {
$temp[$j] = $result[$i - 1][$j - 1] + $result[$i - 1][$j];
}
$temp[$i] = 1;
$result[$i] = $temp;
}
return $result;
}
var_dump(generate(5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment