Skip to content

Commit 212ea0c

Browse files
committed
414_Third_Maximum_Number
1 parent 721169a commit 212ea0c

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ Also, there are open source implementations for basic data structs and algorithm
137137
| 408 | [Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/408_Valid_Word_Abbreviation.py) | Go over abbr and word, O(n) and O(1) |
138138
| 409 | [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/409_Longest_Palindrome.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/409_Longest_Palindrome.java) | Length of Palindrome is always 2n or 2n + 1. So, get all possible 2*n, and choose a single one as 1 if it exists. |
139139
| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/412_Fizz_Buzz.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/412_Fizz_Buzz.java) | 1. From 1 to n, condition check<br>2. Condition check and string connect |
140+
| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/414_Third_Maximum_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/414_Third_Maximum_Number.java) | 1. Keep max 1-3 then compare, O(n) and O(1)<br>2. PriorityQueue, O(n) and O(1) |
140141
| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/415_Add_Strings.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/415_Add_Strings.java) | Two points, careful abour carry, O(n) and O(n) |
141142
| 416 | [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/416_Partition_Equal_Subset_Sum.py) | DP, Check if sum of some elements can be half of total sum, O(total_sum / 2 * n) and O(total_sum / 2) |
142143
| 421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/421_Maximum_XOR_of_Two_Numbers_in_an_Array.py) | Check 0~32 prefix, check if there is x y in prefixes, where x ^ y = answer ^ 1, O(32n) and O(n) |

java/414_Third_Maximum_Number.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public 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+
}

python/414_Third_Maximum_Number.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def thirdMax(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
import Queue
8+
pq = Queue.PriorityQueue(4)
9+
check = set()
10+
for n in nums:
11+
if n in check:
12+
continue
13+
pq.put(n)
14+
check.add(n)
15+
if len(check) > 3:
16+
check.remove(pq.get())
17+
total = len(check)
18+
while total < 3 and total > 1:
19+
total -= 1
20+
return pq.get()

0 commit comments

Comments
 (0)