문제
Start at the top-left corner of a grid with rows by columns cells. Move along cell edges only, one edge right or one edge down at a time, until reaching the bottom-right corner. Count distinct move sequences. The grid has no obstacles.
A grid with 2 rows and 3 columns of cells needs exactly 2 down moves and 3 right moves.
Implement lattice_paths(rows, columns).
Input
rows: The number of rows of cells, not vertices.
columns: The number of columns of cells, not vertices.
Output
Return the number of corner-to-corner routes.
제약 조건
0 ≤ rows, columns ≤ 30 and rows + columns ≤ 60. Zero in one dimension gives a straight route. A 0 × 0 grid has one empty route. Counts fit signed 64-bit.
예시
입력:
rows = 2
columns = 3
예상 출력:
expected = 10
입력:
long long rows = 2;
long long columns = 3;
예상 출력:
long long expected = 10;
입력:
long rows = 2;
long columns = 3;
예상 출력:
long expected = 10;
설명
Every route contains five moves. Choosing which two are down gives ten distinct routes.
입력:
rows = 0
columns = 4
예상 출력:
expected = 1
입력:
long long rows = 0;
long long columns = 4;
예상 출력:
long long expected = 1;
입력:
long rows = 0;
long columns = 4;
예상 출력:
long expected = 1;
설명
There is only the straight route of four right moves.
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
Each route is just an ordering of the required down and right moves.
힌트 2
Count how many ways to choose the positions of one move type among all moves, rather than exploring paths one by one.
힌트 3
Watch the degenerate grids: if one dimension is zero, there is exactly one straight route, and a 0×0 grid still has one empty route.
예상 복잡도
O(min(rows, columns)) time, O(1) extra space
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.