code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

119 Pascal Triangle 2

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const pascalTriangle2 = rowIndex => {
  let result = [[1]]
  while (result.length <= rowIndex) {
    let temp = [1]
    const last = result.length - 1
    for (let i = 1; i < result[last].length; i++) {
      temp.push(result[last][i] + result[last][i - 1])
    }
    temp.push(1)
    result.push(temp)
  }
  return result[rowIndex]
}

console.log(pascalTriangle2(3))

Created 2023-05-07T16:24:50+01:00 · Edit