ปานกลาง 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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก