โจทย์
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
ตัวอย่าง
อินพุต:
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: [" # #", " ## ##", "### ###"].
อินพุต:
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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน