Last Updated: June 7, 2026
This is the upright right-angled triangle turned upside down. The widest row sits at the top, and the point of the triangle ends at the bottom.
The first row has n stars, and each row below has one fewer than the row above. The last row, row n, has a single star. For n = 4, the rows hold 4, 3, 2, and 1 stars, which reads as ****, ***, **, *. Each row is its own string in the returned array.
Row i has n - i + 1 stars. When i is 1, that gives n stars for the top row, and when i is n, it gives 1 star for the bottom row. Getting that formula right is the heart of the problem.
1 <= n <= 100 → n is always at least 1, so the result has at least one row and is never empty. The widest row has at most 100 stars, which stays small.i is simply n - i + 1 stars joined together.An outer loop walks the row numbers from 1 to n. For each row i, the star count is n - i + 1, so an inner loop runs that many times and appends a star on each pass. At the top, i is small, so the count is close to n. As i climbs toward n, the count drops toward 1, producing the shrinking shape.
i from 1 to n, where i is the current row number.n - i + 1.Input:
At i = 1, the count is 4 - 1 + 1 = 4, so the inner loop builds "****" for the first row. At i = 2, the count is 3, giving "***". At i = 3 the count is 2, giving "**", and at i = 4 the count is 1, giving "*". The four rows collected in order form the inverted triangle:
n times, and the inner loop runs up to n times, so the total number of star appends sums to n + (n-1) + ... + 1, which grows with the square of n.n strings whose lengths sum to n + (n-1) + ... + 1 characters, so the result grows with the square of n.The counting-down formula works, but there is a second way to see this shape. An inverted triangle is just an upright triangle with its rows flipped top to bottom. Can we build the upright version and flip it?
The inverted triangle holds the same rows as the upright triangle, just read backward. The upright version lists them shortest to longest as *, **, ***, ****, while the inverted version lists them longest to shortest as ****, ***, **, *.
So build the upright triangle first, where row i has i stars, then reverse the order of the rows. The reversal turns the ascending sequence of lengths into a descending one, which is the inverted shape. This keeps the row-building logic plain and leaves the flipping to a single reverse step.
i from 1 to n and add a string of i stars to the list, building the upright triangle.Input:
The first loop builds the upright triangle row by row, producing ["*", "**", "***", "****"]. The rows climb from one star to four. Reversing this list swaps the first and last rows, then the second and third, leaving the longest row on top and the single star at the bottom:
1 + 2 + ... + n characters, and the reverse step only touches n rows, so the build step dominates and grows with the square of n.n rows whose lengths sum to 1 + 2 + ... + n characters, which grows with the square of n.