समस्या
Flip the right triangle: the first row is the longest, with n stars, and each following row has one star fewer, down to a single *. Counting the outer loop down instead of up is the whole trick.
Implement inverted_right_triangle(n).
Input
n: The number of rows.
Output
Return the rows from top to bottom. Row i (counting from 1) has n − i + 1 * characters.
सीमाएँ
0 ≤ n ≤ 100
उदाहरण
इनपुट:
n = 4
अपेक्षित आउटपुट:
expected = ["****", "***", "**", "*"]
इनपुट:
long long n = 4;
अपेक्षित आउटपुट:
std::vector<std::string> expected = {"****", "***", "**", "*"};
इनपुट:
long n = 4;
अपेक्षित आउटपुट:
string[] expected = new string[] {"****", "***", "**", "*"};
व्याख्या
Rows shrink by one star: "****", "***", "**", "*".
इनपुट:
n = 1
अपेक्षित आउटपुट:
expected = ["*"]
इनपुट:
long long n = 1;
अपेक्षित आउटपुट:
std::vector<std::string> expected = {"*"};
इनपुट:
long n = 1;
अपेक्षित आउटपुट:
string[] expected = new string[] {"*"};
व्याख्या
With one row the triangle is just ["*"].
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
Let the outer loop count down from n to 1.
संकेत 2
Each row has as many stars as the current loop value.
संकेत 3
When n is 0, return an empty list.
अपेक्षित जटिलता
O(n^2) time, O(n^2) extra space
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।