【Lintcode】LRU Cache, Data Stream Median

主要是priority_queue的用法

一个是内置类型优先队列怎么设置小根堆(默认大根堆)

如果是自定义数据结构,有两种办法

1、定义这种数据结构的比较符号,就可以当成内置类型整

2、传进去一个重载()的类,当小于号用,默认还是大根堆,也许传进去的是个callable object都行的吧,我试了一下函数好像不行,不懂,不管了

LRU Cache

class LRUCache{
public:
    // @param capacity, an integer

    int Time;
    typedef int key;
    typedef int time;
    typedef int value;
    typedef pair<key,time> ktpair;

    struct cmp
    {
        bool operator()(ktpair x1,ktpair x2)
        {
            return x1.second > x2.second;
        }
    };

    map<key,value> kv;
    map<key,time> kt;
    priority_queue<ktpair,vector<ktpair>,cmp> q;
    int Cap;

    LRUCache(int capacity) {
        // write your code here
        Time = 0;
        Cap = capacity;
    }

    void insert(int key,int value,int time)
    {
        kv[key] = value;
        kt[key] = time;
        q.push(make_pair(key,time));
    }

    // @return an integer
    int get(int key) {
        // write your code here
        Time++;
        value ret;
        if (kv.find(key) != kv.end())
        {
            int value = kv[key];
            insert(key,value,Time);
            return value;
        }
        else return -1;
    }

    // @param key, an integer
    // @param value, an integer
    // @return nothing
    void set(int key, int value) {
        // write your code here
        Time++;
        if (kv.find(key) == kv.end())
        {
            insert(key,value,Time);
            if (!Cap)
            {
                for(;;)
                {
                    auto x = q.top();
                    auto ckey = x.first;
                    auto ctime = x.second;
                    q.pop();
                    if (kt.find(ckey) == kt.end() || kt[ckey] != ctime) continue;
                    else
                    {
                        kv.erase(ckey);
                        kt.erase(ckey);
                        break;
                    }
                }
            }
            else Cap--;
        }
        else insert(key,value,Time);
    }
};

Data Stream Median

class Solution {
public:
    /**
     * @param nums: A list of integers.
     * @return: The median of numbers
     */
    vector<int> medianII(vector<int> &nums) {
        // write your code here
        priority_queue<int,vector<int>,less<int>> q1;
        priority_queue<int,vector<int>,greater<int>> q2;
        vector<int> ret;
        q1.push(INT_MIN);
        q2.push(INT_MAX);
        for (auto x : nums)
        {
            if (x < q2.top()) q1.push(x);
            else q2.push(x);
            if (q1.size() < q2.size())
            {
                q1.push(q2.top());
                q2.pop();
            }
            if (q1.size() > 1 + q2.size())
            {
                q2.push(q1.top());
                q1.pop();
            }
            ret.push_back(q1.top());
        }
        return ret;
    }
};
时间: 2024-11-08 17:19:20

【Lintcode】LRU Cache, Data Stream Median的相关文章

【LeetCode】LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. set

【LeetCode】LRU Cache 解决报告

插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the ke

【leetcode】LRU Cache(hard)★

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(

【leetcode刷题笔记】LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(

lintcode 1: Data Stream Median

Data Stream Median Numbers keep coming, return the median of numbers at every time a new number added. Have you met this question in a real interview? Example For numbers coming list: [1, 2, 3, 4, 5], return [1, 1, 2, 2, 3]. For numbers coming list:

【LeetCode 题解】 LRU Cache

设计并实现最近最久未使用(Least Recently Used)缓存. 链接:https://oj.leetcode.com/problems/lru-cache/ 题目描述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (wi

【算法】—— LRU算法

LRU原理 LRU(Least recently used,最近最少使用)算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”. 实现1 最常见的实现是使用一个链表保存缓存数据,详细算法实现如下:  1. 新数据插入到链表头部: 2. 每当缓存命中(即缓存数据被访问),则将数据移到链表头部: 3. 当链表满的时候,将链表尾部的数据丢弃. 分析 [命中率] 当存在热点数据时,LRU的效率很好,但偶发性的.周期性的批量操作会导致LRU命中率急剧下

【Lintcode】Median of two Sorted Arrays

class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @return: a double whose format is *.5 or *.0 */ int check(vector<int>& A, vector<int>& B,int k,int m) { int M = A.size(); int N = B.size(); int

【转载】Java Cache系列之Cache概述和Simple Cache

原文地址:http://www.blogjava.net/DLevin/archive/2013/10/15/404770.html 前记:最近公司在做的项目完全基于Cache(Gemfire)构建了一个类数据库的系统,自己做的一个小项目里用过Guava的Cache,以前做过的项目中使用过EHCache,既然和Cache那么有缘,那就趁这个机会好好研究一下Java中的Cache库.在Java社区中已经提供了很多Cache库实现,具体可以参考http://www.open-open.com/13.