MCPcopy
hub / github.com/qiyuangong/leetcode / add

Method add

java/703_Kth_Largest_Element_in_a_Stream.java:14–25  ·  view source on GitHub ↗
(int val)

Source from the content-addressed store, hash-verified

12 }
13
14 public int add(int val) {
15 // add to heaq if it's less then k
16 if (q.size() < k)
17 q.offer(val);
18 else if (q.peek() < val) {
19 // if len(heaq) == k, and val greater than smallest num
20 // then pop smallest num than add val to heap
21 q.poll();
22 q.offer(val);
23 }
24 return q.peek();
25 }
26}
27
28/**

Callers 1

KthLargestMethod · 0.95

Calls 1

peekMethod · 0.45

Tested by

no test coverage detected