समस्या
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
उदाहरण
इनपुट:
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: ["*****", " ***", " *"].
इनपुट:
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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।