aboutsummaryrefslogtreecommitdiff
path: root/third_max_num.c
blob: aa28e16ca5cd3c781eb0fa0858c77dd96fbedead (plain)
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
// 414. Third Maximum Number
#include "leetcode.h"

/*
 * given an integer array 'nums', return the third distinct maximum number in
 * this array. if the third maximum number does not exist, return the maximum
 * number.
 */

int thirdMax(int *nums, int numsSize) {
  int m1, m2, m3, cnt = 0;
  m1 = m2 = m3 = INT_MIN;
  for (int i = 0; i < numsSize; i++)
    if (m1 <= nums[i])
      m1 = nums[i];
  for (int i = 0; i < numsSize; i++)
    if (m2 <= nums[i] && nums[i] < m1)
      m2 = nums[i];
  for (int i = 0; i < numsSize; i++)
    if (m3 <= nums[i] && nums[i] < m2) {
      m3 = nums[i];
      cnt++;
    }
  if (!cnt)
    return m1;
  return m3;
}

int main() {
  int n1[] = {3, 2, 1}, n2[] = {1, 2}, n3[] = {2, 2, 3, 1};
  printf("%d\n", thirdMax(n1, ARRAY_SIZE(n1))); // expect: 1
  printf("%d\n", thirdMax(n2, ARRAY_SIZE(n2))); // expect: 2
  printf("%d\n", thirdMax(n3, ARRAY_SIZE(n3))); // expect: 1
}