首页 > 其他分享 >406. 根据身高重建队列c

406. 根据身高重建队列c

时间:2024-03-11 16:36:35浏览次数:16  
标签:head people 队列 tail 406 int array 身高 peopleSize

折磨折磨,写错一个参数,找半天。

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int divide(int** people,int head,int tail){
    int th=people[head][0],tk=people[head][1];
    while(head<tail){
        while(head<tail && (people[tail][0] < th || (people[tail][0]==th && people[tail][1] > tk ) )) tail--;
        if(head<tail){
            people[head][0]=people[tail][0];
            people[head][1]=people[tail][1];
            head++;
        }
        while(head<tail && (people[head][0] > th ||(people[head][0]==th && people[head][1] <tk ) ) ) head++;
        if(head<tail){
            people[tail][0]=people[head][0];
            people[tail][1]=people[head][1];
            tail--;
        }
    }
    people[head][0]=th;
    people[head][1]=tk;
    return head;
}

void quicksort(int** people,int head,int tail){
    if(head>=tail) return;
    int t=divide(people,head,tail);
    if(t>head) quicksort(people,head,t-1);
    if(t<tail) quicksort(people,t+1,tail);
}

void insert(int** array,int head,int n,int h,int k){
    for(int i=n;i>head;i--){
        array[i][0]=array[i-1][0];
        array[i][1]=array[i-1][1];
    }
    array[head][0]=h;
    array[head][1]=k;
}

int** reconstructQueue(int** people, int peopleSize, int* peopleColSize, int* returnSize, int** returnColumnSizes) {
    *peopleColSize=peopleSize;
    *returnSize=peopleSize;
    int* column=(int*)malloc(sizeof(int)*peopleSize);
    for(int i=0;i<peopleSize;i++) column[i]=2;
    *returnColumnSizes=column;
    quicksort(people,0,peopleSize-1);
    printf("%d  ",peopleSize);
    for(int i=0;i<peopleSize;i++){
        printf("%d %d   ",people[i][0],people[i][1]);
    }
    int** array=(int**)malloc(sizeof(int*)*peopleSize);
    for(int i=0;i<peopleSize;i++) array[i]=(int*)malloc(sizeof(int)*2);
    int n=0;
    for(int i=0;i<peopleSize;i++){
        insert(array,people[i][1],n,people[i][0],people[i][1]);
        n++;
    }
    return array;
}

结果;

标签:head,people,队列,tail,406,int,array,身高,peopleSize
From: https://www.cnblogs.com/llllmz/p/18066439

相关文章

  • 用队列实现栈
    力扣225.用队列实现栈思路:主要是出栈操作,可以使用两个队列,出栈时将入栈队列中数据压入辅助队列中,直到入栈队列只剩下一个数据就是栈顶元素,然后再把辅助队列中元素全部压回入栈队列中,清空辅助队列public:queue<int>que1;queue<int>que2;MyStack(){}......
  • 用栈实现队列
    用栈实现队列,需要两个栈,一个定义为stackIn,另一个定义为stackOut牛客NC76用两个栈实现队列1、队列的push()操作这个直接将数据压入stcakIn,就行voidpush(intnode){ stackIn.push(node);}2、队列的pop()操作这里有两种解法解法一:当stcakOut为空时再将stackIn中的数据......
  • 进程间通信-POSIX 消息队列
    POSIX消息队列POSIX消息队列可以认为是一个消息链表。进程(线程)可以往里写消息,也可以从里面取出消息。可以在不相关的进程之间发送和接收数据。创建(打开)消息队列-mq_open()函数mq_open()函数用于打开或创建一个消息队列,该函数定义如下:#include<mqueue.h>mqd_tmq_open(c......
  • 洛谷P4069 [SDOI2016] 游戏
    题目描述我们要操作的是一条在树上的路径\(s\)->\(t\)。(1)查询\(s\)->\(t\)最大的数字。(2)在\(s\)->\(t\)上增加一个数字,输入\(a\),\(b\),对于路径上的一个点\(u\)增加的数字是\(dis(s,u)\timesa+b\)。解题思路直接查询一条从\(s\)到\(t\)的路径是十分不方便的,所以我们......
  • 第三节:队列相关(滑动窗口最大值、)
    一.        二.        三.         !作       者:Yaopengfei(姚鹏飞)博客地址:http://www.cnblogs.com/yaopengfei/声     明1:如有错误,欢迎讨论,请勿谩骂^_^。声     明2:原创博客请在转载......
  • Unity3D 渲染队列 ZTest与ZWrite详解
    在Unity3D中,渲染队列(RenderingQueue)是一个非常重要的概念,它决定了游戏中各个物体的渲染顺序和优先级。而在渲染队列中,ZTest和ZWrite又是两个关键的参数,它们决定了物体在渲染的过程中如何处理深度测试和深度写入。本文将详细介绍Unity3D中的渲染队列、ZTest和ZWrite的概念,并给出相......
  • 云消息队列 Confluent 版正式上线!
    作者:阿里云消息队列前言在2023年杭州云栖大会上,Confluent成为阿里云技术合作伙伴,在此基础上,双方展开了深度合作,并在今天(3月1日)正式上线“云消息队列Confluent版”。通过将 Confluent在ApacheKafka领域的专业技术及实战经验与阿里云强大的云基础设施及服务体系相结合,......
  • 第三节:队列相关(滑动窗口最大值、)
    一.        二.        三.         !作       者:Yaopengfei(姚鹏飞)博客地址:http://www.cnblogs.com/yaopengfei/声     明1:如有错误,欢迎讨论,请勿谩骂^_^。声     明2:原创博客请在转载......
  • 并发队列
    并发队列目录并发队列为什么要使用队列并发队列简介阻塞队列BlockQueue什么是阻塞队列BlockingQueue主要方法ArrayBlockingQueueLinkedBlockingQueuePriorityBlockingQueueSynchronousQueueSynchronousQueue注意点DelayQueue非阻塞队列ConcurrentLinkedQueue如何选择自己的队列......
  • (34/60)柠檬水找零、根据身高重建队列、用最少数量的箭引爆气球
    柠檬水找零leetcode:860.柠檬水找零贪心法思路遍历一遍数组,只关注面值5、10的钞票的数量每轮判断:如果是5,five++;如果是10,判断还有没有5,有的话five--;如果是20,检查有没有一张10、一张5,ten--,five--。或者三张5,five-=3。贪心:先消耗面值10的钞票,因为它更万能。复杂度分析时间......