【Leetcode笔记】406.根据身高重建队列

news/2024/7/23 17:27:03 标签: leetcode, 笔记, 算法

文章目录

    • 1. 题目要求
    • 2.解题思路
  • 注意
    • 3.ACM模式代码

1. 题目要求

在这里插入图片描述

2.解题思路

首先,按照每个人的身高属性(即people[i][0])来排队,顺序是从大到小降序排列,如果遇到同身高的,按照另一个属性(即people[i][1])来从小到大升序排列。
使用到C++的sort()函数,第三个参数cmp函数自己定义:

static bool cmp(const vector<int>& a, const vector<int>& b)
{
// 使用const关键字来确保在函数内部不会修改a和b
	if(a[0] == b[0]) return a[1] < b[1]; // 象形的记,升序
	return a[0] > b[0];
}

接下来再次遍历people数组,从前到后将每个元素放入到一个二维数组里。
为什么从前到后?
先排身高更高的,后面身高矮的即使因为第二个属性需要插入到前面人的中间去也没关系,反正身高更高的第二个属性不受影响。但是从后到前先排身高矮的可就不行了。

vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        sort (people.begin(), people.end(), cmp);
        vector<vector<int>> que;
        for (int i = 0; i < people.size(); i++) {
            int position = people[i][1];
            que.insert(que.begin() + position, people[i]);
        }
        return que;
    }

时间复杂度要大于O(nlogn + n ^ 2),首先C++里的sort函数的时间复杂度就是O(nlogn),这个排序函数内部并不是单一的快速排序或者是其他的,而是动态改变的,可能一开始数据量较大时先快速排序对半分,等分到后面则使用插入排序;C++的vector是一个动态数组,插入操作是先考虑原来的数组大小够不够,如果不够那就二倍扩容,然后把原数组拷贝到新数组再插入新的元素,所以时间复杂度要大于O(n^2)。
将数组改成链表

vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        sort (people.begin(), people.end(), cmp);
        list<vector<int>> que; // list底层是链表实现,插入效率比vector高的多
        for (int i = 0; i < people.size(); i++) {
            int position = people[i][1]; // 插入到下标为position的位置
            std::list<vector<int>>::iterator it = que.begin();
            while (position--) { // 寻找在插入位置
                it++;
            }
            que.insert(it, people[i]);
        }
        return vector<vector<int>>(que.begin(), que.end());

注意

sort函数的第三个参数是cmp,cmp是一个比较函数,想这样使用 sort (people.begin(), people.end(), cmp);那必须得将cmp声明为静态成员函数,这样就不需要将函数实例化了。

3.ACM模式代码

#include <vector>
#include <algorithm>
#include <list>
#include <iostream> // 用于输出调试

using namespace std;

class Solution {
public:
    static bool cmp(const vector<int>& a, const vector<int>& b) {
        // Sort by height descending, and if heights are same, by k ascending
        if (a[0] == b[0]) {
            return a[1] < b[1]; // Sort by k in ascending order
        } else {
            return a[0] > b[0]; // Sort by height in descending order
        }
    }
    
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        // Sort people array using custom comparator
        sort(people.begin(), people.end(), cmp);
        
        // Use list for efficient insertion
        list<vector<int>> que;
        
        // Insert into list at the specified index (k value)
        for (int i = 0; i < people.size(); i++) {
            int position = people[i][1]; // k value tells us the exact index to insert
            auto it = que.begin();
            advance(it, position); // Move iterator to the correct position
            que.insert(it, people[i]); // Insert person into the list
        }
        
        // Convert list back to vector for returning the result
        return vector<vector<int>>(que.begin(), que.end());
    }
};

int main() {
    Solution sol;
    
    // Example usage:
    vector<vector<int>> people = {{7,0}, {4,4}, {7,1}, {5,0}, {6,1}, {5,2}};
    vector<vector<int>> result = sol.reconstructQueue(people);
    
    // Print the result
    cout << "[";
    for (const auto& person : result) {
        cout << "[" << person[0] << ", " << person[1] << "],";
    }
    cout << "]";
    cout << endl;
    
    return 0;
}

http://www.niftyadmin.cn/n/5546626.html

相关文章

Golang | Leetcode Golang题解之第222题完全二叉树的节点个数

题目&#xff1a; 题解&#xff1a; func countNodes(root *TreeNode) int {if root nil {return 0}level : 0for node : root; node.Left ! nil; node node.Left {level}return sort.Search(1<<(level1), func(k int) bool {if k < 1<<level {return false}…

Msfvenom制作自己的专属Shell

Msfvenom制作自己的专属Shell 如何通过Msfvenom来生成用户自己的专属Shell?有时候我们上传Shell到目标主机后&#xff0c;不仅我们自己可以连接&#xff0c;其他用户也可以连接&#xff0c;有时候会导致我们丢失该Shell&#xff0c;甚至该shell被用户发现并查杀。 实验环境 …

MOJO语言中的字典和哈希表:数据结构的灵活性与效率

MOJO是一种编程语言&#xff0c;它以其独特的语法和对现代编程范式的支持而闻名。在MOJO中&#xff0c;字典&#xff08;也称为哈希表或散列表&#xff09;是一种非常重要的数据结构&#xff0c;它允许开发者以键值对的形式存储和检索数据。本文将深入探讨MOJO语言中的字典和哈…

设计模式使用场景实现示例及优缺点(创建型模式——单例模式、建造者模式、原型模式)

创建型模式 单例模式&#xff08;Singleton Pattern&#xff09; 单例模式&#xff08;Singleton Pattern&#xff09;在Java中的使用场景与在其他编程语言中类似&#xff0c;其主要目的是确保一个类只有一个实例&#xff0c;并提供一个全局的访问点。以下是单例模式的一些常…

【ROS2】初级:客户端-创建和使用插件(C++)

目标&#xff1a;学习使用 pluginlib 创建和加载一个简单的插件。 教程级别&#xff1a;初学者 时间&#xff1a;20 分钟 目录 背景 先决条件 任务 创建基类包创建插件包2.1 插件的源代码2.2 插件声明 XML2.3 CMake 插件声明使用插件构建并运行 摘要 背景 这个教程源自于 http:…

Python入门 2024/7/8

目录 数据容器 dict(字典&#xff0c;映射) 语法 定义字典字面量 定义字典变量 定义空字典 从字典中基于key获取value 字典的嵌套 字典的常用操作 新增元素 更新元素 删除元素 清空字典 获取全部的key 遍历字典 统计字典内的元素数量 练习 数据容器的通用操作…

Gunicorn+Flask+Docker初体验

1. 什么是 Gunicorn? Gunicorn 是一个 Python WSGI 服务器&#xff0c;可以用来部署 Python Web 应用程序。它提供了高性能、高可用性和灵活的配置选项。 2. 什么是 Flask? Flask 是一个轻量级的 Python Web 框架&#xff0c;提供了灵活的路由、模板引擎和请求对象等功能。…

第十八节 LLaVA如何按需构建LORA训练(视觉、语言、映射多个组合训练)

文章目录 前言一、基于llava源码构建新的参数1、添加lora_vit参数2、训练命令脚本设置二、修改源码,构建lora训练1、修改源码-lora训练2、LLM模型lora加载3、VIT模型加载4、权重冻结操作5、结果显示三、实验结果前言 如果看了我前面文章,想必你基本对整个代码有了更深认识。…