Skip to content

Commit

Permalink
Rename arithmetic-slices.cpp to third-maximum-number.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Oct 10, 2016
1 parent 093e8c8 commit 048ff7c
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions C++/third-maximum-number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Time: O(n)
// Space: O(1)

class Solution {
public:
int thirdMax(vector<int>& nums) {
int count = 0;
vector<int> top(3, numeric_limits<int>::min());
for (const auto& num : nums) {
if (num > top[0]) {
top[2] = top[1];
top[1] = top[0];
top[0] = num;
++count;
} else if (num != top[0] && num > top[1]) {
top[2] = top[1];
top[1] = num;
++count;
} else if (num != top[0] && num != top[1] && num >= top[2]) {
top[2] = num;
++count;
}
}

if (count < 3) {
return top[0];
}
return top[2];
}
};

0 comments on commit 048ff7c

Please sign in to comment.