-
Notifications
You must be signed in to change notification settings - Fork 0
/
1403 Minimum Subsuquence in Non-Increasing order.c
83 lines (70 loc) · 1.76 KB
/
1403 Minimum Subsuquence in Non-Increasing order.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
void merge(int * nums, int low, int mid, int high)
{
int * ret_arr = (int *)malloc(sizeof(int) * (high - low + 1));
int ret_arr_index = 0;
int i=0, j=0;
for(i=low, j=mid+1; i<=mid && j<= high; )
{
if(nums[i] <= nums[j])
{
ret_arr[ret_arr_index++] = nums[i++];
}
else
{
ret_arr[ret_arr_index++] = nums[j++];
}
}
while(i<=mid)
{
ret_arr[ret_arr_index++] = nums[i++];
}
while(j<=high)
{
ret_arr[ret_arr_index++] = nums[j++];
}
memcpy(&nums[low], &ret_arr[0], sizeof(int) * (high - low + 1));
free(ret_arr);
}
void merge_sort(int * nums, int low, int high)
{
if(low < high)
{
int mid = (low+high)/2;
merge_sort(nums, low, mid);
merge_sort(nums, mid+1, high);
merge(nums, low, mid, high);
}
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* minSubsequence(int* nums, int numsSize, int* returnSize)
{
int total_sum = 0;
int i=0;
for(i=0; i<numsSize; i++)
{
total_sum += nums[i];
}
merge_sort(nums, 0, numsSize-1);
int total_non_included_sum = total_sum;
int total_included_sum = 0;
int * ret_arr = (int *)malloc(sizeof(int) * numsSize);
int ret_arr_index = 0;
for(i=numsSize-1; i>=0; i--)
{
total_non_included_sum -= nums[i];
total_included_sum += nums[i];
if(total_included_sum <= total_non_included_sum)
{
ret_arr[ret_arr_index++] = nums[i];
}
else
{
ret_arr[ret_arr_index++] = nums[i];
break;
}
}
*returnSize = ret_arr_index;
return ret_arr;
}