-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp045.c
51 lines (51 loc) · 1.3 KB
/
p045.c
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
int jump(int* nums, int numsSize) {
if (numsSize <= 2) return numsSize-1;
int **mem = (int**)malloc(sizeof(int*)*numsSize);
int *memsize = (int*)malloc(sizeof(int)*numsSize);
int i, j;
for (i = 0; i < numsSize; i++)
{
memsize[i] = 0;
}
for (i = 0; i < numsSize-1; i++)
{
nums[i] += i;
if (nums[i] >= numsSize) nums[i] = numsSize-1;
memsize[nums[i]]++;
}
for (i = 0; i < numsSize; i++)
{
if (memsize[i])
{
mem[i] = (int*)malloc(sizeof(int*)*memsize[i]);
memsize[i] = 0;
}
}
for (i = 0; i < numsSize-1; i++)
{
mem[nums[i]][memsize[nums[i]]++] = i;
}
int searchmin = numsSize-1;
int lastsearchmin = numsSize;
int result = 0;
while (searchmin > 0)
{
int tmp = searchmin;
for (i = searchmin; i < lastsearchmin; i++)
{
for (j = 0; j < memsize[i]; j++)
{
if (mem[i][j] < tmp)
{
tmp = mem[i][j];
if (tmp == 0) break;
}
}
if (tmp == 0) break;
}
result++;
lastsearchmin = searchmin;
searchmin = tmp;
}
return result;
}