MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / add

Method add

java/0703-kth-largest-element-in-a-stream.java:11–18  ·  view source on GitHub ↗
(int val)

Source from the content-addressed store, hash-verified

9 }
10
11 public int add(int val) {
12 if (heap.size() < k) heap.offer(val); //for adding the values of the array
13 else if (val > heap.peek()) {
14 heap.poll(); //remove the top element
15 heap.add(val); //add the new element
16 }
17 return heap.peek();
18 }
19}

Callers 1

KthLargestMethod · 0.95

Calls 2

sizeMethod · 0.45
peekMethod · 0.45

Tested by

no test coverage detected