aboutsummaryrefslogtreecommitdiff
path: root/third_max_num.c
diff options
context:
space:
mode:
Diffstat (limited to 'third_max_num.c')
-rw-r--r--third_max_num.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/third_max_num.c b/third_max_num.c
new file mode 100644
index 0000000..aa28e16
--- /dev/null
+++ b/third_max_num.c
@@ -0,0 +1,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
+}