当前位置:网站首页>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);
}
边栏推荐
- Codeforces Round #798 (Div. 2)A~D
- Chapter 6 rebalance details
- 提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)
- (POJ - 1458) common subsequence (longest common subsequence)
- QT realizes window topping, topping state switching, and multi window topping priority relationship
- Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
- 本地可视化工具连接阿里云centOS服务器的redis
- Codeforces - 1526C1&&C2 - Potions
- Problem - 922D、Robot Vacuum Cleaner - Codeforces
- Codeforces - 1526C1&&C2 - Potions
猜你喜欢
软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
Install Jupiter notebook under Anaconda
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
Spark独立集群Worker和Executor的概念
第6章 Rebalance详解
QT realizes window topping, topping state switching, and multi window topping priority relationship
Codeforces Round #801 (Div. 2)A~C
< li> dot style list style type
第五章 Yarn资源调度器
两个礼拜速成软考中级软件设计师经验
随机推荐
Hbuilder X格式化快捷键设置
Oneforall installation and use
Specify the format time, and fill in zero before the month and days
QT realizes window topping, topping state switching, and multi window topping priority relationship
Codeforces Round #799 (Div. 4)A~H
QT实现圆角窗口
Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
Investigation report of bench type Brinell hardness tester industry - market status analysis and development prospect prediction
AcWing——第55场周赛
useEffect,函数组件挂载和卸载时触发
Summary of FTP function implemented by qnetworkaccessmanager
Chapter 5 namenode and secondarynamenode
Chapter 6 rebalance details
AcWing:第56场周赛
Browser print margin, default / borderless, full 1 page A4
antd upload beforeUpload中禁止触发onchange
Market trend report, technical innovation and market forecast of tabletop dishwashers in China
Chapter 5 yarn resource scheduler
Chapter III principles of MapReduce framework
China double brightening film (dbef) market trend report, technical dynamic innovation and market forecast