程式語言 - LeetCode - C - 334. Increasing Triplet Subsequence



參考資訊:
https://www.cnblogs.com/grandyang/p/5194599.html

題目:


解答1:

bool increasingTriplet(int* nums, int numsSize)
{
    int cc = 0;
    int m1 = INT_MAX;
    int m2 = INT_MAX;

    for (cc = 0; cc < numsSize; cc++) {
        if (m1 >= nums[cc]) {
            m1 = nums[cc];
        }
        else if (m2 >= nums[cc]) {
            m2 = nums[cc];
        }
        else {
            return true;
        }
    }

    return false;
}

解答2:

#define min(a, b) a > b ? b : a
#define max(a, b) a > b ? a : b

bool increasingTriplet(int* nums, int numsSize)
{
    int cc = 0;
    int *r_min = malloc(sizeof(int) * numsSize);
    int *r_max = malloc(sizeof(int) * numsSize);

    r_min[0] = nums[0];
    for (cc = 0; cc < (numsSize - 1); cc++) {
        r_min[cc + 1] = min(r_min[cc], nums[cc]);
    }

    r_max[numsSize - 1] = nums[numsSize - 1];
    for (cc = numsSize - 1; cc > 0; cc--) {
        r_max[cc - 1] = max(r_max[cc], nums[cc]);
    }

    for (cc = 0; cc < numsSize; cc++) {
        if ((nums[cc] > r_min[cc]) && (nums[cc] < r_max[cc])) {
            return true;
        }
    }
    free(r_min);
    free(r_max);

    return false;
}