मध्यम 0 हल किए गए 0% स्वीकृति

Double Staircase

Print two staircases of # blocks that face each other across a two-space gap.

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

  • Right-align one part of a row and left-align another
  • Assemble a row from several pieces
  • Avoid trailing spaces in output rows

समस्या

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

Two staircases of height n stand back to back with a gap between them, like the facing pyramids in an old platform game. Row i (counting from 1) has n − i leading spaces, then i # characters, then exactly two spaces, then i more # characters. The left staircase is aligned to the right edge and the right one to the left edge.

Implement double_staircase(n).

Input

n: The height of each staircase.

Output

Return the rows from top to bottom. Every row ends with a #, so there are no trailing spaces.

सीमाएँ

0 ≤ n ≤ 50

उदाहरण

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

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

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

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

व्याख्या

Row 1 is 2 spaces, #, 2 spaces, #; row 3 has no leading spaces: [" # #", " ## ##", "### ###"].

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

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

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

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

व्याख्या

One block on each side of the gap: ["# #"].

कैसे हल करें

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

संकेत 1

Build the left staircase first: it is a right-aligned triangle.

संकेत 2

The gap is always exactly two spaces.

संकेत 3

The right staircase has the same width as the left one on the same row and needs no padding after it.

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

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

संदर्भ

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

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

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

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

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

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