आसान 0 हल किए गए 0% स्वीकृति

Pascal’s Triangle

Build rows by adding neighboring entries from the previous row.

आप क्या सीखेंगे

  • Build each row from the previous one
  • Work with lists of lists
  • Connect rows to binomial coefficients

समस्या

अनुवाद दिखाएँ

The first row is [1]. Each later row starts and ends with 1. Every interior entry is the sum of the two entries immediately above it. Return the first rows rows, beginning with the single 1.

Implement pascals_triangle(rows).

Input

rows: How many rows to generate.

Output

Return a list of rows, starting at the top; each row is a list of integers.

सीमाएँ

0 ≤ rows ≤ 60. Every entry fits a signed 64-bit integer. Zero rows returns an empty list.

उदाहरण

उदाहरण 1
इनपुट:
rows = 4

अपेक्षित आउटपुट:
expected = [
    [1],
    [1, 1],
    [1, 2, 1],
    [1, 3, 3, 1]
]
इनपुट:
long long rows = 4;

अपेक्षित आउटपुट:
std::vector<std::vector<long long>> expected = {
    {1},
    {1, 1},
    {1, 2, 1},
    {1, 3, 3, 1}
};
इनपुट:
long rows = 4;

अपेक्षित आउटपुट:
long[][] expected = new long[][] {
    new long[] {1},
    new long[] {1, 1},
    new long[] {1, 2, 1},
    new long[] {1, 3, 3, 1}
};

व्याख्या

The 2 in row 3 is 1 + 1. The two interior 3s in row 4 are 1 + 2 and 2 + 1.

उदाहरण 2
इनपुट:
rows = 0

अपेक्षित आउटपुट:
expected = []
इनपुट:
long long rows = 0;

अपेक्षित आउटपुट:
std::vector<std::vector<long long>> expected = {};
इनपुट:
long rows = 0;

अपेक्षित आउटपुट:
long[][] expected = new long[][] {};

व्याख्या

No rows are requested, so return an empty list.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Each row starts and ends with 1, and interior values come from the two values above them.

संकेत 2

Build rows one at a time from the previous row rather than recomputing from scratch.

संकेत 3

Handle the zero-row case by returning an empty list immediately.

अपेक्षित जटिलता

O(rows^2) time, O(rows^2) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।