496-The Next Bigger Element

table of Contents

Title description

Given two arrays nums1 and nums2 with no repeated elements, where nums1 is a subset of nums2. Find the next greater value in nums2 for each element in nums1.
The next greater element of the number x in nums1 refers to the first element greater than x to the right of the corresponding position in nums2. If it does not exist, the corresponding position outputs -1.

Here is the quote

Source: LeetCode
Link: https://leetcode-cn.com/problems/next-greater-element-i The
copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

answer

  1. Refer to the official solution
  2. The general idea: Array 1 is a subset of Array 2. Traverse Array 2 once, store the next larger element of all elements in a hash table, and traverse Array 1 again, and change the next one in the order of array 1 The large element is read onto another array. Get the next larger element of each element of array 2: compare the element currently pointed to with the element on the top of the stack. If the element on the top of the stack is smaller than the element of array 2 currently pointed to, then pop the top element of the stack, and the array pointed to at this time The element of 2 is the next larger element, and it is compared until the element in the stack is no smaller than the element currently pointing to array 2. The current pointing element is pushed onto the stack, and the pointer points to the next element in array 2. Loop like this until the elements in array 2 are traversed, and there are no next larger elements in the remaining elements in the stack at this time.
  3. C language implementation

#define maxsize 1000

typedef struct
{
    
    
    char write;//有无写入数据标记
    int key,nextmax;//键值,下一个更大元素
}hashmap;

hashmap* inithashmap(void)//建立并初始化哈希表
{
    
    
    hashmap* hash=(hashmap *)malloc(maxsize*sizeof(hashmap));
    for(int i=0;i<maxsize;i++)
        hash[i].write=0;
    return hash;
}
void writehash(hashmap* h,int key,int nextmax)//写入新的哈希表项
{
    
    
    int p=key%maxsize;
    while(h[p].write!=0)
    {
    
    
        p=(p+1)%maxsize;//使用线性探测法解决冲突
    }
    h[p].write=1;
    h[p].key=key;
    h[p].nextmax=nextmax;
    return;
}
int readhash(hashmap* h,int key)//读取对应键值的哈希表值
{
    
    
    int p=key%maxsize;
    while(h[p].key!=key)
    {
    
    
        p=(p+1)%maxsize;
    }
    return h[p].nextmax;
}


void destroy(hashmap* h)
{
    
    
    free(h);
}

int* nextGreaterElement(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
    
    
    *returnSize=nums1Size;
    int* r_array=(int *)malloc((*returnSize)*sizeof(int));

    hashmap* hm=inithashmap();//哈希表

    int* stack=(int *)malloc(nums2Size*sizeof(int));//栈
    int top=-1;//栈指针

    for(int i=0;i<nums2Size;i++)
    {
    
    
        while(top>-1&&stack[top]<nums2[i])//小于当前比较值则出栈,当前值即为栈顶元素下一个更大元素
        {
    
    
            writehash(hm,stack[top--],nums2[i]);//放入哈希表
        }  
        stack[++top]=nums2[i];//当前值入栈  
    }

    //将剩余的元素写入哈希表,剩余元素没有下一个更大元素
    while(top>-1)
    {
    
    
        writehash(hm,stack[top--],-1);
    }

    //读取哈希表的值如数组
    for(int i=0;i<nums1Size;i++)
    {
    
    
        r_array[i]=readhash(hm,nums1[i]);    
    }

    free(stack);
    destroy(hm);
    
    return r_array;
}

Guess you like

Origin blog.csdn.net/qq_36439722/article/details/112251118