diff --git a/daily/Apr13.cpp b/daily/Apr13.cpp new file mode 100644 index 0000000..d8ab8cd --- /dev/null +++ b/daily/Apr13.cpp @@ -0,0 +1,91 @@ +#include +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>& edges) { + vector 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>& edges) { + vector 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 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>& points, int w) { + // // no need to care about hhe second value + // int xsz = points.size(); + // unordered_map mp; + // for (int i = 0; i < xsz; i++) { + // // int x = points[i]; + // mp[points[i]]++; + // } + // } +}; \ No newline at end of file diff --git a/daily/Apr14.cpp b/daily/Apr14.cpp new file mode 100644 index 0000000..22b4f0f --- /dev/null +++ b/daily/Apr14.cpp @@ -0,0 +1,192 @@ +#include +using namespace std; + +class Solution { + public: +}; + +/** + * @brief We use the vector and list to + * Implement the hashset here + */ +class MyHashSet { + vector> 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 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 cache; + unordered_map 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; + } + } +}; \ No newline at end of file