{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<h1>118. Pascal's Triangle</h1>\n", "<hr>\n", "\n", "<!--Copy Paste Leetcode statement between-->\n", "<p>Given a non-negative integer <em>numRows</em>, generate the first <em>numRows</em> of Pascal's triangle.</p>\n", "\n", "<p><img alt=\"\" src=\"./img1.gif\" style=\"height:240px; width:260px\"><br>\n", "<small>In Pascal's triangle, each number is the sum of the two numbers directly above it.</small></p>\n", "\n", "<p><strong>Example:</strong></p>\n", "\n", "<pre><strong>Input:</strong> 5\n", "<strong>Output:</strong>\n", "[\n", " [1],\n", " [1,1],\n", " [1,2,1],\n", " [1,3,3,1],\n", " [1,4,6,4,1]\n", "]\n", "</pre>\n", "<!--Copy Paste Leetcode statement between-->\n", "\n", "<p> </p>\n", "<a href=\"https://leetcode.com/problems/pascals-triangle/\">Source</a> \n", "<hr>\n", "\n", "<h4>Code</h4>" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def pascal_triangle(numRows):\n", " \"\"\"Dynamic Programming approach.\"\"\"\n", " # Base cases\n", " if numRows == 0:\n", " return []\n", " if numRows == 1:\n", " return [[1]]\n", " \n", " result = [[1], [1, 1]]\n", " for i in range(2, numRows):\n", " result.append([1, i]) # start new row with 1, i\n", " for j in range(2, i):\n", " result[i].append(result[i-1][j-1] + result[i-1][j])\n", " result[i].append(1) # end row with 1\n", " return result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<h4>Check</h4>" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pascal_triangle(5)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1],\n", " [1, 1],\n", " [1, 2, 1],\n", " [1, 3, 3, 1],\n", " [1, 4, 6, 4, 1],\n", " [1, 5, 10, 10, 5, 1],\n", " [1, 6, 15, 20, 15, 6, 1],\n", " [1, 7, 21, 35, 35, 21, 7, 1],\n", " [1, 8, 28, 56, 70, 56, 28, 8, 1],\n", " [1, 9, 36, 84, 126, 126, 84, 36, 9, 1],\n", " [1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1],\n", " [1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1],\n", " [1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1],\n", " [1, 13, 78, 286, 715, 1287, 1716, 1716, 1287, 715, 286, 78, 13, 1],\n", " [1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1]]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pascal_triangle(15)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<hr>\n", "<h4>Follow up:</h4>\n", "<p>Solve it both recursively and iteratively.</p>\n", "\n", "<h4>Code</h4>" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def pascal_triangle(numRows):\n", " \"\"\"Recursive approach.\"\"\"\n", " # Base cases\n", " if numRows == 0:\n", " return []\n", " if numRows == 1:\n", " return [[1]]\n", "\n", " prev = pascal_triangle(numRows-1)\n", " row = [1]*numRows\n", " for i in range(1, numRows-1):\n", " row[i] = prev[-1][i-1]+prev[-1][i]\n", " return prev + [row]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<h4>Check</h4>" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pascal_triangle(5)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1],\n", " [1, 1],\n", " [1, 2, 1],\n", " [1, 3, 3, 1],\n", " [1, 4, 6, 4, 1],\n", " [1, 5, 10, 10, 5, 1],\n", " [1, 6, 15, 20, 15, 6, 1],\n", " [1, 7, 21, 35, 35, 21, 7, 1],\n", " [1, 8, 28, 56, 70, 56, 28, 8, 1],\n", " [1, 9, 36, 84, 126, 126, 84, 36, 9, 1],\n", " [1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1],\n", " [1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1],\n", " [1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1],\n", " [1, 13, 78, 286, 715, 1287, 1716, 1716, 1287, 715, 286, 78, 13, 1],\n", " [1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1]]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pascal_triangle(15)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }