diff options
Diffstat (limited to 'max_product_3num.c')
-rw-r--r-- | max_product_3num.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/max_product_3num.c b/max_product_3num.c new file mode 100644 index 0000000..65f7b19 --- /dev/null +++ b/max_product_3num.c @@ -0,0 +1,42 @@ +// 628. Maximum Product of Three Numbers +#include <limits.h> +#include <math.h> +#include <stdio.h> + +/* + * given an integer array 'nums', find three numbers whose product is maximum + * and return the maximum product + */ + +int maximumProduct(int *nums, int nums_size) { + int min1, min2, max1, max2, max3, num; + min1 = min2 = INT_MAX; + max1 = max2 = max3 = INT_MIN; + for (int i = 0; i < nums_size; i++) { + num = nums[i]; + if (min1 >= num) { + min2 = min1; + min1 = num; + } else if (min2 >= num) { + min2 = num; + } + if (max1 <= num) { + max3 = max2; + max2 = max1; + max1 = num; + } else if (max2 <= num) { + max3 = max2; + max2 = num; + } else if (max3 <= num) { + max3 = num; + } + } + return fmax(min1 * min2 * max1, max1 * max2 * max3); +} + +int main() { + int n1[] = {1, 2, 3}, n2[] = {1, 2, 3, 4}, n3[] = {-1, -2, -3}; + printf("%d\n", maximumProduct(n1, 3)); // expect: 6 + printf("%d\n", maximumProduct(n2, 4)); // expect: 24 + printf("%d\n", maximumProduct(n3, 3)); // expect: -6 +} |