【每日刷题】Day184

server/2025/3/1 13:05:20/

【每日刷题】Day184

🥕个人主页:开敲🍉

🔥所属专栏:每日刷题🍍

🌼文章目录🌼

1. 1700. 无法吃午餐的学生数量 - 力扣(LeetCode)

2. 146. LRU 缓存 - 力扣(LeetCode)

1. 1700. 无法吃午餐的学生数量 - 力扣(LeetCode)

//思路:队列

//本题要点在于如何判断剩下的学生无法吃午餐,其实无非就是两种情况:

//当满足这两种情况的任意一种时,剩余的学生就无法吃到午餐。

class Solution {

int flag = 0;

public:

    int countStudents(vector<int>& students, vector<int>& sandwiches)

    {

        int sum = 0;//这里用sum变量来判断两种情况

        queue<int> student;

        queue<int> sandwiche;

        for(auto& i : students)

        {

            student.push(i);

            sum+=i;

        }

        for(auto& i : sandwiches)

            sandwiche.push(i);

        while(true)

        {

            if(student.front()==sandwiche.front())

            {

                sum-=student.front();

                student.pop();

                sandwiche.pop();

            }

            else

            {

                int tmp = student.front();

                student.pop();

                student.push(tmp);

            }

            if(student.empty()) break;

//sum==(sandwiche.front()+student.size())    判断的是情况①

//sum<sandwiche.front()    判断的是情况②

            if((sum==(sandwiche.front()+student.size()))||sum<sandwiche.front())

                break;

        }

        return student.size();

    }

};

2. 146. LRU 缓存 - 力扣(LeetCode)

//思路:双向链表+哈希表

//哈希表用于查询 key 是否已经存储;双向链表用于进行插入节点以及更新使用过的节点:保证最久没有使用过的节点处于链表末尾

class LRUCache

{

    struct ListNode//链表节点

    {

        int _key,_val;

        ListNode* _next;

        ListNode* _prev;

        ListNode():_key(-1),_val(-1),_next(nullptr),_prev(nullptr)

        {}

        ListNode(int key,int val):_key(key),_val(val),_next(nullptr),_prev(nullptr)

        {}

    };

public:

    LRUCache(int capacity)

    {

        _head = new ListNode();

        _tail = new ListNode();

        _head->_next = _tail;//头尾节点采用 "哨兵位" 形式

        _tail->_prev = _head;

        _capacity = capacity;

    }

   

    int get(int key)

    {

        if(ma.count(key))//如果要获取的key存在,则返回对应的val

        {

            ListNode* curr = ma[key];

            DeleteNode(curr);

            MoveToHead(curr);//同时,将节点更新到链表头部,表示当前节点为最新访问过的

            return ma[key]->_val;

        }

        return -1;//不存在则返回-1

    }

   

    void put(int key, int value)

    {

        if(!ma.count(key))//不存在key,新插入节点

        {

            ListNode* newnode = new ListNode(key,value);

            MoveToHead(newnode);//同时更新节点到链表头部,表示当前节点为最新访问过的

            ma[key] = newnode;//写入哈希表中

            _size++;//更新当前队列长度

        }

        else//存在key,则更新val

        {

            ListNode* curr = ma[key];//获取已经存在的key对应的节点

            curr->_val = value;

            DeleteNode(curr);//因为key对应的节点已经存在于队列中,因此更新时需要将原本旧节点所在位置的前后节点更新

            MoveToHead(curr);//将节点更新到链表头部

        }

        if(_size>_capacity)//超出容量,删除尾部最久未使用的key对应的节点

        {

            ListNode* tail = GetTail();//因为使用的是"哨兵位"模式,因此tail->_prev指向的就是链表尾部节点

            ma.erase(tail->_key);

            DeleteNode(tail);

            delete tail;

            _size--;

        }

    }

//获取尾部节点:最久未访问的节点

    ListNode* GetTail()

    {

        return _tail->_prev;

    }

//删除节点

    void DeleteNode(ListNode* curr)

    {

        curr->_prev->_next = curr->_next;

        curr->_next->_prev = curr->_prev;

    }

//将节点更新到链表头部

    void MoveToHead(ListNode* curr)

    {

        curr->_prev = _head;

        curr->_next = _head->_next;

        _head->_next->_prev = curr;

        _head->_next = curr;

    }

private:

    int _capacity = 0;

    int _size = 0;

    ListNode* _head;

    ListNode* _tail;

    unordered_map<int,ListNode*> ma;//哈希表 key 映射 结点指针

};


http://www.ppmy.cn/server/171552.html

相关文章

极简Redis速成学习

redis是什么&#xff1f; 是一种以键值对形式存储的数据库&#xff0c;特点是基于内存存储&#xff0c;读写快&#xff0c;性能高&#xff0c;常用于缓存、消息队列等应用情境 redis的五种数据类型是什么&#xff1f; 分别是String、Hash、List、Set和Zset&#xff08;操作命…

AF3 pair_sequences函数解读

AlphaFold3 msa_pairing模块的pair_sequences函数的核心目标是基于 MSA(多序列比对)中的物种信息,在多条链之间建立 MSA 配对索引,从而帮助 AlphaFold3 捕捉共进化信息,提升蛋白复合物预测的准确性。函数pair_sequences 通过调用 _make_msa_df、 _create_species_dict 以…

正大杯攻略|量表类问卷数据分析基本步骤

在量表类问卷研究领域&#xff0c;分析变量之间的影响关系是基础且常用的手段。一般先提出关于自变量 X 对因变量 Y 影响关系的假设&#xff0c;随后运用合适的统计方法进行验证&#xff0c;挖掘二者间规律&#xff0c;进而得出结论&#xff0c;为研究发展提供建议。具体分析步…

页面加载速度,如何优化提升?

页面加载速度&#xff0c;如何优化提升&#xff1f; 咱来好好唠唠页面加载速度这事儿&#xff0c;再说说怎么把它提上去。 页面加载速度是咋回事儿 页面加载速度啊&#xff0c;就好比你去餐厅吃饭&#xff0c;从你坐下点餐到饭菜端上桌的时间。在网页里&#xff0c;就是你在…

DeepSeek掘金——调用DeepSeek API接口 实现智能数据挖掘与分析

调用DeepSeek API接口:实现智能数据挖掘与分析 在当今数据驱动的时代,企业和开发者越来越依赖高效的数据挖掘与分析工具来获取有价值的洞察。DeepSeek作为一款先进的智能数据挖掘平台,提供了强大的API接口,帮助用户轻松集成其功能到自己的应用中。本文将详细介绍如何调用D…

layui 获取select值和文本

在表单中&#xff0c;使用layui渲染select <select name"pan" lay-filter"down_link_name" id"down_link_name"><option value"">网盘名称</option><option value"01">诚通</option><opt…

SERPENTINE Tools

Tools Here we present the tools created within the SERPENTINE project. They are available from the general SERPENTINE repository at GitHub, and can also be run on the SERPENTINE Hub server without the need to install Python locally. 此处展示SERPENTINE项目组…

postman上一个接口返回值作为下一个接口的入参

1.在第一个接口中提取响应数据 假设接口返回以下数据&#xff1a; {"total": 2,"rows": [{"createBy": null,"createTime": "2024-07-21 12:54:24","updateBy": null,"updateTime": "2024-09-09…