-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathThirdMaximumNumber414.java
More file actions
156 lines (140 loc) · 4.27 KB
/
ThirdMaximumNumber414.java
File metadata and controls
156 lines (140 loc) · 4.27 KB
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
/**
* Given a non-empty array of integers, return the third maximum number in this
* array. If it does not exist, return the maximum number.
* The time complexity must be in O(n).
*
* Example 1:
* Input: [3, 2, 1]
* Output: 1
* Explanation: The third maximum is 1.
*
* Example 2:
* Input: [1, 2]
* Output: 2
* Explanation: The third maximum does not exist, so the maximum (2) is
* returned instead.
*
* Example 3:
* Input: [2, 2, 3, 1]
* Output: 1
* Explanation: Note that the third maximum here means the third maximum
* distinct number. Both numbers with value 2 are both considered as
* second maximum.
*
*/
public class ThirdMaximumNumber414 {
public static int thirdMax(int[] arr) {
// non-empty array, no need for checking empty
Stack<Integer> st = new Stack<>();
for (int i: arr) {
Stack<Integer> temp = new Stack<>();
while (!st.empty() && st.peek() <= i) {
temp.push(st.pop());
}
st.push(i);
while (!temp.empty() && st.size() < 3) {
int t = temp.pop();
if (!st.empty() && st.peek() == t) continue;
st.push(t);
}
while (st.size() > 3) {
st.pop();
}
}
if (st.size() == 3) return st.peek();
while (st.size() > 1) {
st.pop();
}
return st.peek();
}
public static int thirdMax2(int[] arr) {
PriorityQueue<Integer> q = new PriorityQueue();
// non empty array
q.add(arr[0]);
for (int i=1; i<arr.length; i++) {
int num = arr[i];
if (q.size() >= 3 && q.peek() >= num) continue;
if (q.contains(num)) continue;
if (q.size() >= 3) q.poll();
q.add(num);
}
// exits
if (q.size() == 3) return q.peek();
// not exits
while (q.size() > 1) q.poll();
return q.peek();
}
/**
* https://leetcode.com/problems/third-maximum-number/discuss/90190/Java-PriorityQueue-O(n)-+-O(1)
*/
public int thirdMax3(int[] nums) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
Set<Integer> set = new HashSet<>();
for (int i : nums) {
if (!set.contains(i)) {
pq.offer(i);
set.add(i);
if (pq.size() > 3) {
set.remove(pq.poll());
}
}
}
if (pq.size() < 3) {
while (pq.size() > 1) {
pq.poll();
}
}
return pq.peek();
}
/**
* https://leetcode.com/problems/third-maximum-number/discuss/90202/Java-neat-and-easy-understand-solution-O(n)-time-O(1)-space
*/
public int thirdMax4(int[] nums) {
Integer max1 = null;
Integer max2 = null;
Integer max3 = null;
for (Integer n : nums) {
if (n.equals(max1) || n.equals(max2) || n.equals(max3)) continue;
if (max1 == null || n > max1) {
max3 = max2;
max2 = max1;
max1 = n;
} else if (max2 == null || n > max2) {
max3 = max2;
max2 = n;
} else if (max3 == null || n > max3) {
max3 = n;
}
}
return max3 == null ? max1 : max3;
}
// public static int thirdMax4(int[] arr) {
// int[] temp = new int[3];
// Arrays.fill(temp, Integer.MIN_VALUE);
// int c = 0;
// for (int i: arr) {
// int minIdx = -1;
// int min = Integer.MAX_VALUE;
// boolean dup = false;
// for (int k=0; k<3; k++) {
// if (temp[k] == i) {
// dup = true;
// break;
// }
// if (min > i) {
// min = i;
// minIdx = k;
// }
// }
// if (!dup && minIdx != -1) {
// c++;
// temp[minIdx] = i;
// }
// }
// if (c >= 3) {
// return Math.min(Math.min(temp[0], temp[1]), temp[2]);
// } else {
// return Math.max(Math.max(temp[0], temp[1]), temp[2]);
// }
// }
}