aboutsummaryrefslogtreecommitdiff
path: root/Computer_Science/leetcode/62-unique_paths.c
blob: 6f613fa902a49719cf24b0348edccff12fa43888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int uniquePaths(int m, int n)
{
	int i, j;
	int *matrix = malloc(sizeof(int) * m * n);

	if(m <= 1 || n <= 1)
		return 1;

	for(i = 0; i < m; i++) {
		for(j = 0; j < n; j++) {
			*(matrix + i * n + j) = 1;
		}
	}

	for(i = 1; i < m; i++) {
		for(j = 1; j < n; j++) {
			*(matrix + i * n + j) =
				*(matrix + i * n + j-1)
				+ *(matrix + (i - 1) * n + j);
		}
	}

	return *(matrix + m * n - 1);
}

int main()
{
	printf("%d\n", uniquePaths(3, 7));
}