쉬움 0명 해결 정답률 0%

Pascal’s Triangle

Build rows by adding neighboring entries from the previous row.

배우게 될 내용

  • Build each row from the previous one
  • Work with lists of lists
  • Connect rows to binomial coefficients

문제

번역본 보기

The first row is [1]. Each later row starts and ends with 1. Every interior entry is the sum of the two entries immediately above it. Return the first rows rows, beginning with the single 1.

Implement pascals_triangle(rows).

Input

rows: How many rows to generate.

Output

Return a list of rows, starting at the top; each row is a list of integers.

제약 조건

0 ≤ rows ≤ 60. Every entry fits a signed 64-bit integer. Zero rows returns an empty list.

예시

예시 1
입력:
rows = 4

예상 출력:
expected = [
    [1],
    [1, 1],
    [1, 2, 1],
    [1, 3, 3, 1]
]
입력:
long long rows = 4;

예상 출력:
std::vector<std::vector<long long>> expected = {
    {1},
    {1, 1},
    {1, 2, 1},
    {1, 3, 3, 1}
};
입력:
long rows = 4;

예상 출력:
long[][] expected = new long[][] {
    new long[] {1},
    new long[] {1, 1},
    new long[] {1, 2, 1},
    new long[] {1, 3, 3, 1}
};

설명

The 2 in row 3 is 1 + 1. The two interior 3s in row 4 are 1 + 2 and 2 + 1.

예시 2
입력:
rows = 0

예상 출력:
expected = []
입력:
long long rows = 0;

예상 출력:
std::vector<std::vector<long long>> expected = {};
입력:
long rows = 0;

예상 출력:
long[][] expected = new long[][] {};

설명

No rows are requested, so return an empty list.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Each row starts and ends with 1, and interior values come from the two values above them.

힌트 2

Build rows one at a time from the previous row rather than recomputing from scratch.

힌트 3

Handle the zero-row case by returning an empty list immediately.

예상 복잡도

O(rows^2) time, O(rows^2) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.