-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathNo215.kth-largest-element-in-an-array.cs
180 lines (148 loc) · 3.47 KB
/
No215.kth-largest-element-in-an-array.cs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Difficulty:
* Medium
*
* Desc:
* Find the kth largest element in an unsorted array.
* Note that it is the kth largest element in the sorted order, not the kth distinct element.
*
* Example 1:
* Input: [3,2,1,5,6,4] and k = 2
* Output: 5
*
* Example 2:
* Input: [3,2,3,1,2,4,5,5,6] and k = 4
* Output: 4
*
* Note:
* You may assume k is always valid, 1 ≤ k ≤ array's length.
*/
public interface IBinaryHeapEntity
{
public int Value { get; }
}
public interface IBinaryHeap<T>
{
public int Count { get; }
public void Push(T item);
public T Pop();
public T Top();
public void Exchange(T item, int pos);
}
public class BinaryHeapEntity : IBinaryHeapEntity
{
private int Val;
public BinaryHeapEntity(int value)
{
this.Val = value;
}
public int Value
{
get
{
return this.Val;
}
}
}
// 最小堆
class BinaryHeap<T> : IBinaryHeap<T>
where T : IBinaryHeapEntity
{
private readonly List<T> heap;
public BinaryHeap()
{
this.heap = new List<T>();
}
public int Count
{
get
{
return this.heap.Count;
}
}
public void Push(T entity)
{
this.heap.Add(entity);
this.SortWithFather(this.Count);
}
public T Top()
{
if (this.Count == 0)
{
return default;
}
return this.heap[0];
}
public T Pop()
{
if (this.Count == 0)
{
return default;
}
T lastEntity = this.heap[this.Count - 1];
this.heap[this.Count - 1] = this.heap[0];
this.heap[0] = lastEntity;
T res = this.heap[this.Count - 1];
this.heap.RemoveAt(this.Count - 1);
if (this.Count > 0)
{
this.SortWithChildren(1);
}
return res;
}
public void Exchange(T entity, int pos)
{
this.heap[pos] = entity;
this.SortWithChildren(pos + 1);
}
private int GetValue(int index)
{
return this.heap[index].Value;
}
private void SortWithFather(int cPos)
{
int fPos = cPos / 2;
T target = this.heap[cPos - 1];
while (cPos > 1 && target.Value < this.GetValue(fPos - 1))
{
this.heap[cPos - 1] = this.heap[fPos - 1];
cPos = fPos;
fPos = cPos / 2;
}
this.heap[cPos - 1] = target;
}
private void SortWithChildren(int fPos)
{
int cPos = fPos * 2;
T target = this.heap[fPos - 1];
while (cPos - 1 < this.Count)
{
if (cPos < this.Count && this.GetValue(cPos - 1) > this.GetValue(cPos))
{
cPos += 1;
}
if (this.GetValue(cPos - 1) >= target.Value)
{
break;
}
this.heap[fPos - 1] = this.heap[cPos - 1];
fPos = cPos;
cPos = fPos * 2;
}
this.heap[fPos - 1] = target;
}
}
public class Solution {
public int FindKthLargest(int[] nums, int k) {
IBinaryHeap<IBinaryHeapEntity> heap = new BinaryHeap<IBinaryHeapEntity>();
for (int i = 0; i < k; i += 1) {
heap.Push(new BinaryHeapEntity(nums[i]));
}
for (int i = k; i < nums.Length; i += 1) {
if (nums[i] > heap.Top().Value) {
heap.Exchange(new BinaryHeapEntity(nums[i]), 0);
}
}
return heap.Top().Value;
}
}