Example #1
0
int LRUCacheMiss(vector<int> input, int maxSize) {
	typedef list<int> LI;
	typedef list<int>::iterator LII;		// key iterator 
	typedef unordered_map<int, LII> MLII;	// key, (key iterator)

	LI cache;
	MLII check;
	int count = 0;
	for (int i = 0; i < input.size(); i++) {
		if (check.count(input[i]) > 0) {
			// found in the LRU cache.
			cache.erase(check[input[i]]);
			cache.push_front(input[i]);
			check[input[i]] = cache.begin();
		}
		else {
			// not found, miss happens.
			count++;
			cache.push_front(input[i]);
			check[input[i]] = cache.begin();
			if (cache.size() > maxSize) {
				int key = cache.back();
				cache.pop_back();
				check.erase(key);
			}
		}
	}
	return count;
}
Example #2
0
    /**
     * @param numbers: Give an array numbersbers of n integer
     * @param target: you need to find four elements that's sum of target
     * @return: Find all unique quadruplets in the array which gives the sum of
     *          zero.
     */
    LLI fourSum(LI nums, int target) {
        sort(nums.begin(), nums.end());

        LLI res;
        LI set;

        fourSum(res, set, nums, 0, target);

        return res;
    }
Example #3
0
 void set(int key, int value) {
     auto it = cache.find(key);
     if(it!=cache.end()) {
         update(it);
     } else {
         if(used.size()==cap) {
             cache.erase(used.back());
             used.pop_back();
         }
         used.push_front(key);
     }
     cache[key] = {value, used.begin()};
 }
Example #4
0
    void combine(LLI &results, LI &set, LI &candidates, int k, int target) {
        int sum = 0;
        for (int x : set)
            sum += x;

        if (sum == target) {
            LI sorted = set;
            sort(sorted.begin(), sorted.end());
            results.push_back(sorted);
            return;
        }

        if (target < sum)
            return;

        for (int i = k; i < candidates.size(); ++i) {
            set.push_back(candidates[i]);
            combine(results, set, candidates, i, target);
            set.pop_back();
        }
    }
Example #5
0
 void update(HPIL::iterator it){
     int key = it->first;
     lru.erase(it->second.second);
     lru.push_front(key);
     it->second.second = lru.begin();
 }
Example #6
0
 void update(HIPII::iterator it) {
     int key = it->first;
     used.erase(it->second.second);
     used.push_front(key);
     it->second.second = used.begin();
 }
Example #7
0
LI::iterator DLL::find(int val){
    LI::iterator it;
    for(it=dt.begin();it!=dt.end();++it)
        if((*it) == val) return it;
    return it;
}
Example #8
0
void DLL::print(){
    LI::iterator it;
    for(it=dt.begin();it!=dt.end();++it)
        cout << (*it) << "->";
    cout << "NULL" << endl;
}