-
Notifications
You must be signed in to change notification settings - Fork 14
/
Compare_k-th_largest_in_heap.cpp
69 lines (62 loc) · 2.42 KB
/
Compare_k-th_largest_in_heap.cpp
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
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
#include <cassert>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
void compare_kth_largest_heap_helper(const vector<int>& max_heap, int k, int x,
int idx, int* larger, int* equal);
// @include
// -1 means smaller, 0 means equal, and 1 means larger.
int compare_kth_largest_heap(const vector<int>& max_heap, int k, int x) {
int larger = 0, equal = 0;
compare_kth_largest_heap_helper(max_heap, k, x, 0, &larger, &equal);
return larger >= k ? 1 : (larger + equal >= k ? 0 : -1);
}
void compare_kth_largest_heap_helper(const vector<int>& max_heap, int k,
int x, int idx, int* larger,
int* equal) {
if (*larger >= k || idx >= max_heap.size() || max_heap[idx] < x) {
return;
} else if (max_heap[idx] == x) {
if (++*equal >= k) {
return;
}
} else { // max_heap[idx] > x.
++*larger;
}
compare_kth_largest_heap_helper(max_heap, k, x, (idx << 1) + 1, larger,
equal);
compare_kth_largest_heap_helper(max_heap, k, x, (idx << 1) + 2, larger,
equal);
}
// @exclude
void small_test() {
vector<int> max_heap = {10, 2, 9, 2, 2, 8, 8, 2, 2, 2, 2, 7, 7, 7, 7};
assert(1 == compare_kth_largest_heap(max_heap, 3, 2)); // kth is larger.
assert(1 == compare_kth_largest_heap(max_heap, 6, 2)); // kth is larger.
}
int main(int argc, char* argv[]) {
small_test();
// 5
// 4 5
// 4 4 4 3
// 4
vector<int> max_heap = {5, 4, 5, 4, 4, 4, 3, 4};
int k, x;
if (argc == 3) {
k = atoi(argv[1]), x = atoi(argv[2]);
int res = compare_kth_largest_heap(max_heap, k, x);
cout << (res == -1 ? "smaller" : (res == 0 ? "equal" : "larger")) << endl;
} else {
assert(-1 == compare_kth_largest_heap(max_heap, 1, 6)); // expect smaller
assert(0 == compare_kth_largest_heap(max_heap, 1, 5)); // expect equal
assert(0 == compare_kth_largest_heap(max_heap, 6, 4)); // expect equal
assert(0 == compare_kth_largest_heap(max_heap, 3, 4)); // expect equal
assert(-1 == compare_kth_largest_heap(max_heap, 8, 4)); // expect smaller
assert(1 == compare_kth_largest_heap(max_heap, 2, 4)); // expect larger
assert(1 == compare_kth_largest_heap(max_heap, 2, 3)); // expect larger
}
return 0;
}