-
Notifications
You must be signed in to change notification settings - Fork 0
/
Distribute Candies
150 lines (139 loc) · 4.71 KB
/
Distribute Candies
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
LeetCode 575:
Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain.
Example 1:
Input: candies = [1,1,2,2,3,3]
Output: 3
Explanation:
There are three different kinds of candies (1, 2 and 3), and two candies for each kind.
Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too.
The sister has three different kinds of candies.
Example 2:
Input: candies = [1,1,2,3]
Output: 2
Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1].
The sister has two different kinds of candies, the brother has only one kind of candies.
Note:
The length of the given array is in range [2, 10,000], and will be even.
The number in given array is in range [-100,000, 100,000].
Solution:
Language: C++
Tag: hashset sort 2Pointer
*/
class Solution {
public:
#define min(a, b) (a < b ? a : b)
int distributeCandies(vector<int>& candies) {
/*
// 1. hashset: O(n) space + O(n) time
int max_cand = candies.size() / 2;
int num_kinds = 0;
unordered_set<int> hash;
for (int i = 0; i < candies.size(); ++i) {
if (hash.find(candies[i]) != hash.end()) {
continue;
}
hash.insert(candies[i]);
++num_kinds;
}
return min(num_kinds, max_cand);
*/
// 2. Sort + 2 pointer: O(nlogn) time + O(1) space
int size = candies.size();
int num_kinds = 0;
sort(candies.begin(), candies.end(), less<int>());
//quicksort(candies, 0, size - 1);
//heapsort(candies);
//int *temp = new int[size];
//mergesort(candies, 0, size - 1, temp);
int i = 0;
while (i < size) {
int j = i + 1;
while (j < size && candies[j] == candies[i]) {
++j;
}
++num_kinds;
i = j;
}
return min(num_kinds, size / 2);
}
void quicksort(vector<int> &nums, int start, int end) {
if (start >= end) {
return;
}
int left = start, right = end, pivot = nums[(left + right) / 2];
while (left <= right) {
while (left <= right && nums[left] < pivot) {
++left;
}
while (left <= right && nums[right] > pivot) {
--right;
}
if (left <= right) {
swap(nums[left++], nums[right--]);
}
}
quicksort(nums, start, right);
quicksort(nums, left, end);
}
void mergesort(vector<int> &nums, int start, int end, int *temp) {
if (start >= end) {
return;
}
int mid = start + (end - start) / 2;
mergesort(nums, start, mid, temp);
mergesort(nums, mid + 1, end, temp);
merge(nums, start, end, temp);
}
void merge(vector<int> &nums, int start, int end, int *temp) {
int mid = start + (end - start) / 2;
int left = start, right = mid + 1, idx = start;
while (left <= mid && right <= end) {
if (nums[left] <= nums[right]) {
temp[idx++] = nums[left++];
} else {
temp[idx++] = nums[right++];
}
}
while (left <= mid) {
temp[idx++] = nums[left++];
}
while (right <= end) {
temp[idx++] = nums[right++];
}
for (int i = start; i <= end; ++i) {
nums[i] = temp[i];
}
}
void heapsort(vector<int> &nums) {
int size = nums.size();
for (int i = (size - 1) / 2; i >= 0; --i) {
siftdown(nums, i, size);
}
while (size > 0) {
swap(nums[0], nums[--size]);
siftdown(nums, 0, size);
}
int left = 0, right = nums.size() - 1;
while (left < right) {
swap(nums[left++], nums[right--]);
}
}
void siftdown(vector<int> &nums, int father, int size) {
while (father < size) {
int min_idx = father;
int left = father * 2 + 1, right = left + 1;
if (left < size && nums[left] < nums[min_idx]) {
min_idx = left;
}
if (right < size && nums[right] < nums[min_idx]) {
min_idx = right;
}
if (father == min_idx) {
break;
}
swap(nums[father], nums[min_idx]);
father = min_idx;
}
}
};