-
Notifications
You must be signed in to change notification settings - Fork 0
/
1046. Last Stone Weight.c
72 lines (61 loc) · 1.64 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
/** C
int compare (const void * a, const void * b) // 一開始想要用快排,但後來選用從大到小排序
{
return ( *(int*)a - *(int*)b );
}
void largeto(int *stones, int array_size)
{
int change = 0;
for (int i = 0; i < array_size; i++) {
for (int j = 0; j < array_size; j++) {
if (stones[i] > stones[j] && i != j) {
change = stones[i];
stones[i] = stones[j];
stones[j] = change;
}
}
}
}
int lastStoneWeight(int* stones, int stonesSize){
int heavy1 = 0, heavy2 = 0, number = stonesSize;
int *parray = stones;
if (!stonesSize)
return 0;
if (stonesSize == 1)
return *stones;
//qsort(stones, stonesSize, sizeof(int), compare);
largeto(stones, stonesSize);
while (stonesSize > 1) {
heavy1 = stones[0];
heavy2 = stones[1];
if (heavy1 == heavy2) {
stones[0] = -1;
stones[1] = -1;
stonesSize -= 2;
}else {
stones[0] = -1;
stones[1] = heavy1 - heavy2;
stonesSize--;
}
largeto(stones, number);
}
// for (int i = 0; i < number; i++)
// printf("%d ",stones[i]);
for (int i = 0; i < number; i++)
if (stones[i] != -1)
return stones[i];
return 0;
}
/**
int cmp(int *a, int *b) {
return *a - *b;
}
int lastStoneWeight(int* stones, int stonesSize){
while (stonesSize > 1) {
qsort(stones, stonesSize, sizeof(int), cmp);
stones[stonesSize - 2] = stones[stonesSize-1] - stones[stonesSize - 2];
stonesSize--;
}
return stones[0];
}
*/