Likou 739. Daily Temperature-Violent Method and Monotonous Stack

739. Daily temperature

Please generate a new list based on the daily temperature list. The output of the corresponding location is: at least the number of days to wait in order to observe a higher temperature. If the temperature does not rise after this, please use 0 instead.

For example, given a list temperatures = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].

提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 
[30, 100] 范围内的整数。

Method 1: Initial brute force method (timeout)

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* dailyTemperatures(int* T, int TSize, int* returnSize){
    
    
    int *a = (int*)malloc(sizeof(int)*TSize);
    for(int i=0;i<TSize;i++)
    {
    
    
        int sum = 0;
        for(int j=i;j<TSize;j++)
        {
    
    
            if(T[j]>T[i])
            {
    
    
                a[i]=sum;
                break;
            }
            if(j==TSize-1)
            {
    
    
                a[i]=0;
            }
            else
            {
    
    
                sum++;
            }
        }


    }
    *returnSize=TSize;
    return a;
}

Method 2: Optimized brute force method

Because the last element directly returns 0.

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* dailyTemperatures(int* T, int TSize, int* returnSize){
    
    
    int *a = (int*)malloc(sizeof(int)*TSize);
    for(int i=0;i<TSize-1;i++)
    {
    
    
        int sum = 1;
        for(int j=i+1;j<TSize;j++)
        {
    
    
            if(T[j]>T[i])
            {
    
    
                a[i]=sum;
                break;
            }
            if(j==TSize-1)
            {
    
    
                a[i]=0;
            }
            else
            {
    
    
                sum++;
            }
        }
    }
    a[TSize-1]=0;
    *returnSize=TSize;
    return a;
}

Method 3: Monotonic Stack

First attach a small video of a great god in the solution of the force button:

Video made by Brother Wu, a programmer

We can build two dynamic arrays to simulate the stack.

Set up the temp array to store the subscripts in the original array corresponding to each temperature, and the res array to store the answers we really want .

The reason we use temp array to store the temperature corresponding to the index of the original array , because this is the case because the storage can be found subscript index quickly, and because the subscript know, we can find an array quickly from the temperature of his correspondence For the temperature , you only need to nest the temp array in the subscript of the temperature array.

Because it can be seen from the question that once "the temperature will not rise after this" appears, then res should be assigned a value of 0, so in order to avoid trouble, we can start by initializing the entire array to 0 , so that there is no need to do it later Assignment operation.
From the animation, we can see that we use two arrays to simulate stacking and popping.

That is:
when the stack is empty, we need to import the temp array into the stack, that is, directly into the stack; when the stack is not empty, we need to consider two situations:
1. If the corresponding temperature at this time is greater than that stored in the stack temperature. That is, the elements outside the stack to enter the stack are larger than the elements on the top of the stack.
Then you can pop the top element of the stack, calculate the subscript difference between the two in the temperature array, and assign it to the res array, and then compare the element below the top of the stack with the element outside the stack that is going to enter the stack. If it is greater than, then continue popping and the same assignment operation until there is no element in the stack.
If it is less than the stack is not popped, let the outside of the stack be pushed into the stack.
2. If it is not greater than, then it is directly put into the stack.

So the code is as follows:

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* dailyTemperatures(int* T, int TSize, int* returnSize){
    
    
    int *temp = (int*)malloc(sizeof(int)*TSize);
    memset(temp,0,sizeof(int)*TSize);
    int *res = (int*)malloc(sizeof(int)*TSize);
    memset(res,0,sizeof(int)*TSize);
    int index = -1;//此为代表栈内元素的下标
    for(int i=0;i<TSize;i++)
    {
    
    
        while(index>=0&&T[i]>T[temp[index]])//满足情况一时
        {
    
    
            res[temp[index]]=i-temp[index];
            index--;//index--其实模拟了出栈并且也成功让栈顶紧挨着的下面的元素进行下一轮的比较。
            //因为栈中下标提前了,所以在赋值时会出现覆盖的情况,即模拟了出栈
        }
        index++;//要入栈,所以要更新一下栈内下标
        temp[index]=i;//入栈操作,也是借助“覆盖”操作对一些元素完成出栈
    }
    *returnSize=TSize;
    return res;
}

And for "* returnSize=TSize ":
Because you returned the pointer, but you need to tell the outside that the pointer points to the length of the memory. This is the purpose of returnSize.

Guess you like

Origin blog.csdn.net/xiangguang_fight/article/details/112581646