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

Method thirdMax

java/414_Third_Maximum_Number.java:2–15  ·  view source on GitHub ↗
(int[] nums)

Source from the content-addressed store, hash-verified

1public class Solution {
2 public int thirdMax(int[] nums) {
3 PriorityQueue<Integer> pq = new PriorityQueue<>(3);
4 Set<Integer> set = new HashSet<>();
5 for (int i : nums) {
6 if (set.contains(i)) continue;
7 pq.offer(i);
8 set.add(i);
9 if (pq.size() > 3) set.remove(pq.poll());
10 }
11 while (pq.size() < 3 && pq.size() > 1) {
12 pq.poll();
13 }
14 return pq.peek();
15 }
16}

Callers

nothing calls this directly

Calls 3

addMethod · 0.45
removeMethod · 0.45
peekMethod · 0.45

Tested by

no test coverage detected