Skip to content

Commit

Permalink
Apr13 and Apr14 result push
Browse files Browse the repository at this point in the history
  • Loading branch information
aucker committed Apr 14, 2024
1 parent bdc87d2 commit bc99df2
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 0 deletions.
91 changes: 91 additions & 0 deletions daily/Apr13.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
/**
* @brief Just like map, change the value of edges
* Time: O(n), only one traverse
* @param n, edges
* @return int
*/
int findChampion(int n, vector<vector<int>>& edges) {
vector<int> ans(n);
for (auto& edge : edges) ans[edge[1]]++;

int champion = -1;
for (int i = 0; i < n; i++) {
if (ans[i] == 0) {
if (champion != -1) return -1;
champion = i;
}
}
return champion;
}

// Same idea, same way
int findChampion1(int n, vector<vector<int>>& edges) {
vector<int> is_weak(n);
for (auto& e : edges) {
is_weak[e[1]] = true; // not chapion, in degree > 0
}

int ans = -1;
for (int i = 0; i < n; i++) {
if (is_weak[i]) {
continue;
}
if (ans != -1) {
return -1; // more than one champion
}
ans = i;
}
return ans;
}
};

class MovingAverage {
int size;
double sum;
queue<int> q;

public:
// MovingAverage(int size) {
// this->size = size;
// this->sum = 0.0;
// }

// this 2 types of construtor works same way
MovingAverage(int size) : size(size), sum(0.0) {}

double next(int val) {
if (size == q.size()) {
sum -= q.front();
q.pop();
}
q.emplace(val);
sum += val;
return sum / q.size();
}
};

class Contest138 {
public:
int scoreOfString(string s) {
int res = 0;
for (int i = 1; i < s.size(); i++) {
res += abs(s[i] - s[i - 1]);
}
return res;
}

// int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {
// // no need to care about hhe second value
// int xsz = points.size();
// unordered_map<int, int> mp;
// for (int i = 0; i < xsz; i++) {
// // int x = points[i];
// mp[points[i]]++;
// }
// }
};
192 changes: 192 additions & 0 deletions daily/Apr14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
};

/**
* @brief We use the vector and list to
* Implement the hashset here
*/
class MyHashSet {
vector<list<int>> data;
static const int base = 769;
static int hash(int key) { return key % base; }

public:
MyHashSet() : data(base) {}

void add(int key) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); it++) {
if ((*it) == key) {
return;
}
}
data[h].push_back(key);
}

void remove(int key) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); it++) {
if ((*it) == key) {
data[h].erase(it);
return;
}
}
}

bool contains(int key) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); it++) {
if ((*it) == key) return true;
}
return false;
}
};

struct Node {
int key, val;
int freq;
Node *prev, *next;
Node() : key(0), val(0), prev(nullptr), next(nullptr) {}
Node(int key_, int val_)
: key(key_), val(val_), prev(nullptr), next(nullptr) {}
Node() : key(-1), val(-1), freq(0), prev(nullptr), next(nullptr) {}
Node(int key_, int val_)
: key(key_), val(val_), freq(1), prev(nullptr), next(nullptr) {}
};

struct FreqList {
int freq;
Node *vhead, *vtail;
FreqList(int freq_) : freq(freq_), vhead(new Node()), vtail(new Node()) {
vhead->next = vtail;
vtail->prev = vhead;
}
};

class LRUCache {
unordered_map<int, Node*> cache;
int size;
Node *head, *tail;

public:
LRUCache(int capacity_) : size(capacity_) {
head = new Node();
tail = new Node();
head->next = tail;
tail->prev = head;
}

void addHead(Node* node) {
node->next = head->next;
node->prev = head->prev;
head->next->prev = node;
head->next = node;
}

void deleteNode(Node* node) {
node->prev->next = node->next;
node->next->prev = node->prev;
}

void popTail() {
Node* node = tail->prev;
deleteNode(node);
cache.erase(node->key);
}

int get(int key) {
int res = -1;
if (cache.find(key) != cache.end()) {
// find
Node* node = cache[key];
res = node->val;
deleteNode(node);
addHead(node);
}
return res;
}

void put(int key, int value) {
if (size == 0) return;
if (get(key) != -1) {
cache[key]->val = value;
} else {
if (size == cache.size()) {
popTail();
}
Node* node = new Node(key, value);
cache[key] = node;
addHead(node);
}
}
};

class LFUCache {
unordered_map<int, Node*> cache;
unordered_map<int, FreqList*> freq_map;
int size;
int min_freq;

public:
LFUCache(int capacity_) : size(capacity_) {}

bool isEmpty(FreqList* list) {
return list->vhead->next == list->vtail ? true : false;
}

void addHead(Node* node) {
int freq = node->freq;
if (freq_map.find(freq) == freq_map.end()) {
freq_map[freq] = new FreqList(freq);
}
FreqList* list = freq_map[freq];
node->next = list->vhead->next;
node->prev = list->vhead->next->prev;
list->vhead->next->prev = node;
list->vhead->next = node;
}

void deleteNode(Node* node) {
node->prev->next = node->next;
node->next->prev = node->prev;
}

void popTail() {
Node* node = freq_map[min_freq]->vtail->prev;
deleteNode(node);
cache.erase(node->key);
}

int get(int key) {
int res = -1;
if (cache.find(key) != cache.end()) {
Node* node = cache[key];
res = node->val;
deleteNode(node);
node->freq++;
if (isEmpty(freq_map[min_freq])) min_freq++;
addHead(node);
}
return res;
}

void put(int key, int value) {
if (size == 0) return;
if (get(key) != -1) {
// exist
cache[key]->val = value;
} else {
if (size == cache.size()) {
popTail();
}
Node* node = new Node(key, value);
addHead(node);
min_freq = 1;
cache[key] = node;
}
}
};

0 comments on commit bc99df2

Please sign in to comment.