414. Third Maximum Number

Question Given an integer array of nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum count. Example 1: Input: nums = [3,2,1] Output: 1 Explanation: The first distinct maximum is 3. The second distinct maximum is 2. The third distinct maximum is 1. Example 2: Input: nums = [1,2] Output: 2 Explanation: The first distinct maximum is 2. The second distinct maximum is 1. There is no third distinct maximum, so maximum (2) is returned instead. Example 3: Input: nums = [2,2,3,1] Output: 1 Explanation: The first distinct maximum is 3. The second distinct maximum is 2 (both 2's are counted together because they have the same value). The third distinct maximum is 1. Solution class Solution { public: int thirdMax(vector& nums) { sort(nums.begin(),nums.end(),greater()); if(nums.size()<3){ return nums[0]; } int m=nums[0]; int c=2; for(int i=1;i

Comments

Popular posts from this blog

141. Linked List Cycle

67. Add Binary

88. Merge Sorted Array