Problema
Draw a right-aligned pyramid like the one in Super Mario: row i (counting from 1) has height − i spaces followed by i # characters.
Implement pyramid_rows(height).
Input
height: The number of rows.
Output
Return the rows from top to bottom.
Vincoli
0 ≤ height ≤ 50
Esempi
Input:
height = 3
Output atteso:
expected = [" #", " ##", "###"]
Input:
long long height = 3;
Output atteso:
std::vector<std::string> expected = {" #", " ##", "###"};
Input:
long height = 3;
Output atteso:
string[] expected = new string[] {" #", " ##", "###"};
Spiegazione
Rows are " #", " ##" and "###": each row has one more block and one less space.
Input:
height = 1
Output atteso:
expected = ["#"]
Input:
long long height = 1;
Output atteso:
std::vector<std::string> expected = {"#"};
Input:
long height = 1;
Output atteso:
string[] expected = new string[] {"#"};
Spiegazione
A single row with one block: "#".
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
Each row combines leading spaces with a growing run of hash characters.
Suggerimento 2
Build rows one by one from top to bottom so the alignment stays correct.
Suggerimento 3
If the height is zero, return no rows rather than an empty-looking pyramid.
Complessità attesa
O(height^2) time, O(height^2) extra space
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.