-
Notifications
You must be signed in to change notification settings - Fork 0
/
1046 Last Stone Weight.c
84 lines (75 loc) · 1.77 KB
/
1046 Last Stone Weight.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
void merge(int arr[], int low, int mid, int high)
{
int * tmp_arr = (int *)malloc(sizeof(int) * (high - low + 1));
int tmp_arr_index = 0;
int i=0, j=0;
for(i=low, j=mid+1; i<=mid && j<=high; )
{
if(arr[i] >= arr[j])
{
tmp_arr[tmp_arr_index++] = arr[i++];
}
else
{
tmp_arr[tmp_arr_index++] = arr[j++];
}
}
while(i<=mid)
{
tmp_arr[tmp_arr_index++] = arr[i++];
}
while(j<=high)
{
tmp_arr[tmp_arr_index++] = arr[j++];
}
memcpy(&arr[low], tmp_arr, sizeof(int) * tmp_arr_index);
free(tmp_arr);
}
void merge_sort(int arr[], int low, int high)
{
if(low < high)
{
int mid = (low + high)/2;
merge_sort(arr, low, mid);
merge_sort(arr, mid+1, high);
merge(arr, low, mid, high);
}
}
int lastStoneWeight(int* stones, int stonesSize)
{
merge_sort(stones, 0, stonesSize-1);
int i=0;
int prev_item = stones[0];
for(i=1; i<stonesSize; )
{
if(stones[i] > prev_item)
{
prev_item = stones[i] - prev_item;
stones[i] = prev_item;
merge_sort(&stones[i], 0, stonesSize-1-i);
prev_item = stones[i];
i++;
}
else if(stones[i] == prev_item)
{
if(i+1 >= stonesSize)
{
prev_item = 0;
}
else
{
prev_item = stones[++i];
}
i++;
}
else
{
prev_item = prev_item - stones[i];
stones[i] = prev_item;
merge_sort(&stones[i], 0, stonesSize-1-i);
prev_item = stones[i];
i++;
}
}
return prev_item;
}