当前位置:网站首页>Tencent interview algorithm question
Tencent interview algorithm question
2022-07-06 16:41:00 【Dog egg L】
describe
Design LRU( Recently at least use ) Cache structure , The structure is sized at construction time , Assume the size is K, And has the following two functions
set(key, value): Will record (key, value) Insert the structure
get(key): return key Corresponding value value
Tips :
1. Some key Of set or get Once the operation occurs , Think of this key The most commonly used record of , Then the cache will be refreshed .
2. When the size of the cache exceeds K when , Remove the least frequently used records .
3. Enter a two-dimensional array and K, Two dimensional array, each dimension has 2 A or 3 A digital , The first 1 Numbers are opt, The first 2,3 Numbers are key,value
if opt=1, The next two integers key, value, Express set(key, value)
if opt=2, Next an integer key, Express get(key), if key Not present or removed , Then return to -1
For each opt=2, Output an answer
4. In order to distinguish the cache key And value, In the cache described below key use "" Package No
Advanced : Can you be in O(1) The time complexity of set and get operation
Example 1
Input :
[[1,1,1],[1,2,2],[1,3,2],[2,1],[1,4,4],[2,2]],3
Return value :
[1,-1]
explain :
[1,1,1], first 1 Express opt=1, want set(1,1), the (1,1) Insert cache , The cache is {“1”=1}
[1,2,2], first 1 Express opt=1, want set(2,2), the (2,2) Insert cache , The cache is {“1”=1,“2”=2}
[1,3,2], first 1 Express opt=1, want set(3,2), the (3,2) Insert cache , The cache is {“1”=1,“2”=2,“3”=2}
[2,1], first 2 Express opt=2, want get(1), Return is [1], because get(1) operation , Cache update , The cache is {“2”=2,“3”=2,“1”=1}
[1,4,4], first 1 Express opt=1, want set(4,4), the (4,4) Insert cache , But the cache has reached its maximum capacity 3, Remove the least frequently used {“2”=2}, Insert {“4”=4}, The cache is {“3”=2,“1”=1,“4”=4}
[2,2], first 2 Express opt=2, want get(2), Can't find , Return is [1,-1]
Example 2
Input :
[[1,1,1],[1,2,2],[2,1],[1,3,3],[2,2],[1,4,4],[2,1],[2,3],[2,4]],2
Return value :
[1,-1,-1,3,4]
#include<unordered_map>
class Solution {
public:
/**
* lru design
* @param operators int integer vector<vector<>> the ops
* @param k int integer the k
* @return int integer vector
*/
int capacity;
list<pair<int,int>> lrulst;
unordered_map<int,list<pair<int,int>>::iterator > lruhash;
vector<int> LRU(vector<vector<int> >& operators, int k) {
vector<int> result; capacity = k;
result.reserve(operators.size());
if(k!=0)
{
for(const vector<int>& opt: operators)
{
if(opt[0]==1) set(opt[1],opt[2]);
else if(opt[0]==2) result.push_back(get(opt[1]));
}
}
return result;
}
void set(int key,int val){
auto iter = lruhash.find(key);
if(iter == lruhash.end())
{
if(capacity == lrulst.size())
{
lruhash.erase(lrulst.back().first);
lrulst.pop_back();
}
}
else lrulst.erase(iter->second);
lrulst.push_front({key,val});
lruhash[key] = lrulst.begin();
}
int get(int key){
auto iter = lruhash.find(key);
if(iter == lruhash.end()) return -1;
int val = iter->second->second;
lrulst.erase(iter->second);
lrulst.push_front(*iter->second);
return val;
}
};
List flip
subject : List flip . Give a list and a number k, For example, linked list 1→2→3→4→5→6,k=2, After flipping 2→1→4→3→6→5, if k=3, After flipping 3→2→1→6→5→4, if k=4, After flipping 4→3→2→1→5→6, To program Node* RotateList(Node* list, size_t k). Tips : This problem is an upgraded variant of the reverse linked list .
Algorithmic thought :
This problem is an upgraded variant of the reverse linked list , We can first follow this linked list with k It is divided into several small linked lists , Then flip the small linked list , Finally, link the small chain list , In turn , When the length of the small linked list is less than k when , Connect it directly behind the linked list .
Code implementation :
#include <iostream>
#include <stdio.h>
#include <cstring>
typedef int DataType;
typedef struct ListNode
{
DataType _data;
struct ListNode* _next;
}Node,*pNode,*pList;
void Init(pList* pplist)
{
assert(pplist);
*pplist = NULL;
}
pNode BuyNode(DataType x)
{
pNode pnode = (pNode)malloc(sizeof(Node));
if (pnode == NULL)
{
perror("malloc");
return NULL;
}
pnode->_data = x;
pnode->_next = NULL;
return pnode;
}
void Push(pList* pplist,DataType x)
{
pNode NewNode = BuyNode(x);
if (*pplist == NULL)
{
*pplist = NewNode;
}
else
{
pNode cur = *pplist;
while (cur->_next)
{
cur = cur->_next;
}
cur->_next = NewNode;
}
}
pNode Reverse(pList plist)
{
pNode cur = plist;
if(cur == NULL)
return NULL;
if (cur->_next)
{
Reverse(cur->_next);
}
printf("%d ", cur->_data);
return cur;
}
pNode getLastNode(pList plist)
{
pNode pHead = plist;
while (pHead->_next != NULL)
{
pHead = pHead->_next;
}
return pHead;
}
pNode SwapListByK(pList plist, size_t k)
{
pNode pnode = plist;
pNode pNewNode;
pNode pNextNode;
pNode LastNode = NULL;
pNode tmp = NULL;
size_t pos;
if (k <= 1)
return plist;
plist = NULL;
while (pnode)
{
pos = 0;
pNewNode = pnode;
while (pnode && pos < k - 1)
{
tmp = pNewNode;
pnode = pnode->_next;
if (pnode == NULL)
{
break;
}
pos++;
}
if (pnode == NULL)
{
return tmp; // If you need to reverse the deficiency k Elements , Directly connected to the back
}
if (pnode)
{
pNextNode = pnode->_next;
pnode->_next = NULL;
if (LastNode != NULL)
{
LastNode->_next = NULL;
}
pNewNode = Reverse(pNewNode);
if (plist == NULL)
plist = pNewNode;
else
LastNode->_next = pNewNode;
pnode = getLastNode(pNewNode);
pnode->_next = pNextNode;
LastNode = pnode;
pnode = pNextNode;
}
else
{
break;
}
}
return pnode;
}
void Printf(pList plist)
{
pList cur = plist;
while (cur)
{
printf("%d ", cur->_data);
cur = cur->_next;
}
printf(" NULL\n");
}
void Test()
{
pList plist;
Init(&plist);
Push(&plist, 1);
Push(&plist, 2);
Push(&plist, 3);
Push(&plist, 4);
Push(&plist, 5);
Push(&plist, 6);
Push(&plist, 7);
Printf(plist);
pNode ret = SwapListByK(plist, 3);
Printf(ret);
}
边栏推荐
- Date plus 1 day
- Chapter III principles of MapReduce framework
- Local visualization tools are connected to redis of Alibaba cloud CentOS server
- 875. Leetcode, a banana lover
- 第 300 场周赛 - 力扣(LeetCode)
- js封装数组反转的方法--冯浩的博客
- Tert butyl hydroquinone (TBHQ) Industry Research Report - market status analysis and development prospect forecast
- Codeforces Round #800 (Div. 2)AC
- Effet d'utilisation, déclenché lorsque les composants de la fonction sont montés et déchargés
- Discussion on QWidget code setting style sheet
猜你喜欢
sublime text 代码格式化操作
解决Intel12代酷睿CPU单线程只给小核运行的问题
第5章 NameNode和SecondaryNameNode
OneForAll安装使用
视频压缩编码和音频压缩编码基本原理
QT realizes window topping, topping state switching, and multi window topping priority relationship
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
The concept of spark independent cluster worker and executor
Codeforces round 797 (Div. 3) no f
随机推荐
Summary of game theory
Input can only input numbers, limited input
Remove the border when input is focused
Problem - 1646C. Factorials and Powers of Two - Codeforces
业务系统从Oracle迁移到openGauss数据库的简单记录
SQL快速入门
OneForAll安装使用
Market trend report, technical innovation and market forecast of double-sided foam tape in China
Bidirectional linked list - all operations
生成随机密码/验证码
Log statistics (double pointer)
业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事
Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
< li> dot style list style type
QT implementation fillet window
(lightoj - 1370) Bi shoe and phi shoe (Euler function tabulation)
Codeforces - 1526C1&&C2 - Potions
图图的学习笔记-进程
js时间函数大全 详细的讲解 -----阿浩博客
图像处理一百题(1-10)