leetcode算法设计第三周作业 LRU缓存问题

leetcode算法设计第三周作业
LRU缓存问题:
https://leetcode.com/problems/lru-cache/description/
Difficulty:Hard
Total Accepted:204.4K
Total Submissions:955.9K

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

Follow up:
Could you do both operations in O(1) time complexity?

Example:

LRUCache cache = new LRUCache( 2 /* capacity */ );
cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // returns 1
cache.put(3, 3);    // evicts key 2
cache.get(2);       // returns -1 (not found)
cache.put(4, 4);    // evicts key 1
cache.get(1);       // returns -1 (not found)
cache.get(3);       // returns 3
cache.get(4);       // returns 4

题目看起来不难,有操作系统基础知识的对这个应该都不陌生。
主要难在优化和对于容器的选择上,这个关系到代码的效率以及关键函数能否达到o(1)的时间复杂度。
下面是我的第一版答案,虽然通过了但时间较慢,用了结构体数组,每次查找删除的时间复杂度都为o(n),有点臃肿。

class LRUCache {
private:
	struct PointStruct
	{
		int key = INT16_MIN;
		int value = INT16_MIN;
	};
	int capacity;
	PointStruct* ps;
	int currentSize;
public:
	LRUCache(int capacity) {
		ps = new PointStruct[capacity];
		this->capacity = capacity;
		currentSize = 0;
	}
	int get(int key) {
		for (int i = currentSize-1; i >=0; i--) {
			if (ps[i].key == key) {
				PointStruct temp;
				temp.key = ps[i].key;
				temp.value = ps[i].value;
				for (int j = i; j <currentSize - 1; j++) {
					ps[j] = ps[j + 1];
				}
				ps[currentSize - 1] = temp;
				return temp.value;
			}
		}
		return -1;//not found
	}
	void put(int key, int value) {
		for (int i = currentSize - 1; i >= 0; i--) {
			if (ps[i].key == key) {
				ps[i].value = value;
				PointStruct temp;
				temp = ps[i];
				for (int j = i; j < currentSize-1; j++) {
					ps[j] = ps[j + 1];
				}
				ps[currentSize - 1] = temp;
				return;
			}
		}
		if(currentSize==capacity){
			for (int i = 0; i < currentSize - 1; i++) {
				ps[i] = ps[i + 1];
			}
			ps[capacity - 1].key = key;
			ps[capacity - 1].value = value;
		}
		else if (currentSize > capacity) {
			cout << "error" << endl;
		}
		else {
			ps[currentSize].key = key;
			ps[currentSize].value = value;
			currentSize++;
		}
		return;
	}
};

见到dalao们的高分参考后,发现有两点差距:

  • 在代码段后面可以加上以下的lambda解析式提高输入输出效率,这个方法的关键在于将c++中的cin和cout的缓冲区关闭,可以显著提升效率避免时间和速度的损失。
static const auto io_sync_off = []()
{
	// turn off sync
	std::ios::sync_with_stdio(false);
	// untie in/out streams
	std::cin.tie(nullptr);
	return nullptr;
}();
  • 结合利用list和map:
 	list <pair<int, int>> l;
    unordered_map <int, list <pair<int, int>>::iterator> mp;
  • 使用list的l.splice(l.begin(), l, x, next(x));可以将x挪到最前面。
  • 使用mp.find()可以在o(1)之内找到对应的值,将两者结合起来,可以做到查找和删除的时候都可以达到o(1)的时间复杂度。
    其中,unordered_map用的是哈希函数映射,而不是map的红黑树,在查找的速度上效率更高。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值