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
Ogni riga combina spazi iniziali con una sequenza crescente di caratteri #.
Suggerimento 2
Costruisci le righe una per una dall’alto verso il basso, così l’allineamento resta corretto.
Suggerimento 3
Se l’altezza è zero, restituisci nessuna riga invece di una piramide che sembra vuota.
Complessità attesa
tempo O(height^2), spazio aggiuntivo O(height^2)
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.