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

Inverted Right Triangle

Print a right triangle upside down: n stars on the first row, one on the last.

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

  • Run a loop from n down to 1
  • Relate the row index to the number of symbols
  • Return a pattern as a list of rows

समस्या

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

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

उदाहरण

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

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

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

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

व्याख्या

Rows shrink by one star: "****", "***", "**", "*".

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

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

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

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

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

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