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

Inverted Pyramid

Print a centered pyramid of stars upside down, with the widest row on top.

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

  • Count spaces up while stars count down
  • Express a row's width with a formula in the row number
  • Avoid trailing spaces in output rows

समस्या

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

Turn the centered pyramid over: the top row is the widest, with 2n − 1 stars, and each row below is two stars narrower and shifted one space further right, until the last row holds a single *. Row i (counting from 1) has i − 1 leading spaces followed by 2(n − i) + 1 stars.

Implement inverted_pyramid(n).

Input

n: The number of rows.

Output

Return the rows from top to bottom, without trailing spaces.

सीमाएँ

0 ≤ n ≤ 100

उदाहरण

उदाहरण 1
इनपुट:
n = 3

अपेक्षित आउटपुट:
expected = ["*****", " ***", "  *"]
इनपुट:
long long n = 3;

अपेक्षित आउटपुट:
std::vector<std::string> expected = {"*****", " ***", "  *"};
इनपुट:
long n = 3;

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

व्याख्या

Row 1 has 0 spaces and 5 stars, row 2 has 1 space and 3 stars, row 3 has 2 spaces and 1 star: ["*****", " ***", " *"].

उदाहरण 2
इनपुट:
n = 1

अपेक्षित आउटपुट:
expected = ["*"]
इनपुट:
long long n = 1;

अपेक्षित आउटपुट:
std::vector<std::string> expected = {"*"};
इनपुट:
long n = 1;

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

व्याख्या

One row with one star: ["*"].

कैसे हल करें

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

संकेत 1

Write the rows for n = 3 and count the spaces and stars on each.

संकेत 2

Spaces grow by one per row while stars shrink by two.

संकेत 3

The last row always has exactly one star.

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

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

संदर्भ

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

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

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

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

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