当前位置:网站首页>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);
	
}
     
    
   
原网站

版权声明
本文为[Dog egg L]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060920336198.html