Commit 2356b4c5 authored by Sadman Kazi's avatar Sadman Kazi 🎸

Added some old solutions

parent 12520e09
#include <set>
#include <map>
#include <vector>
#include <queue>
using namespace std;
//Definition for undirected graph.
struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
};
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (node == NULL) return NULL;
UndirectedGraphNode* res = new UndirectedGraphNode(node->label);
std::map<int, UndirectedGraphNode*> nodes;
nodes[res->label] = res;
std::queue<UndirectedGraphNode*> bfs_queue;
std::set<UndirectedGraphNode*> visited_nodes;
bfs_queue.push(node);
while (!bfs_queue.empty()) {
UndirectedGraphNode* current_node = bfs_queue.front();
bfs_queue.pop();
visited_nodes.insert(current_node);
for (auto iter = current_node->neighbors.begin(); iter!= current_node->neighbors.end(); iter++) {
// check if the node already exists
auto find_node = nodes.find((*iter)->label);
if (find_node == nodes.end()) {
nodes[(*iter)->label] = new UndirectedGraphNode((*iter)->label);
}
// add it to the current nodes child:
nodes[current_node->label]->neighbors.push_back(nodes[(*iter)->label]);
if ((*iter)->label!=current_node->label && visited_nodes.find(*iter) == visited_nodes.end())
bfs_queue.push(*iter);
}
}
return res;
}
};
#include <iostream>
#include <unordered_map>
#include <list>
class LRUCache {
public:
LRUCache(int capacity) : cap(capacity) {
//list = new LinkedList();
}
int get(int key) {
auto iter = data.find(key);
// check if the key existed
if (iter != data.end()) {
makeHead(iter);
return iter->second.first;
} else {
return -1;
}
}
void put(int key, int value) {
auto iter = data.find(key);
if (iter != data.end()) {
makeHead(iter);
} else {
if (data.size() == cap) {
data.erase(list.back());
list.pop_back();
}
list.push_front(key);
}
data[key] = { value, list.begin() };
}
~LRUCache() {
list.clear();
data.clear();
}
private:
std::unordered_map<int, std::pair<int, std::list<int>::iterator> > data;
//LinkedList* list;
std::list<int> list;
int cap;
void makeHead(std::unordered_map<int, std::pair< int, std::list<int>::iterator> >::iterator iter) {
list.erase(iter->second.second);
list.push_front(iter->first);
iter->second.second = list.begin();
}
};
int main () {
LRUCache cache( 2 /* capacity */ );
cache.put(1, 1);
cache.put(2, 2);
std::cout << cache.get(1) << " == 1" << std::endl;
cache.put(3, 3); // evicts key 2
std::cout << cache.get(2) << " == -1"<< std::endl; // returns -1 (not found)
cache.put(4, 4); // evicts key 1
std::cout << cache.get(1) << " == -1"<< std::endl; // returns -1 (not found)
std::cout << cache.get(3) << " == 3"<< std::endl; // returns 3
std::cout << cache.get(4) << " == 4"<< std::endl; // returns 4
}
#include <cctype>
#include <iostream>
#include <bitset>
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t res = 0;
for (int i = 0; i < 32; i++) {
res = res | (n >> i
return res;
}
};
int main () {
Solution s;
std::cout << std::bitset<32>(4) << std::endl;
std::cout << std::bitset<32>(s.reverseBits(4)) << std::endl;
}
#include <set>
#include <vector>
#include <iostream>
using namespace std;
class Solution {
private:
vector<set<int> > subsets;
public:
void setUnion(int xset, int yset) {
subsets[xset].insert(subsets[yset].begin(), subsets[yset].end());
subsets[yset].insert(subsets[xset].begin(), subsets[xset].end());
}
//returns the index of the set that the vertex is in
int find(int vertex) {
for (int i = 0; i < subsets.size(); i++) {
if (subsets[i].find(vertex) != subsets[i].end()) {
std::cout << "found " << vertex << " in " << i << std::endl;
return i;
}
}
return -1;
}
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
vector<int> edge_rem;
for (int i = 0; i < edges.size(); i++) {
set<int> temp;
if (find(edges[i][0]) == -1) {
temp.insert(edges[i][0]);
subsets.push_back(temp);
temp.clear();
}
if (find(edges[i][1]) == -1) {
temp.insert(edges[i][1]);
subsets.push_back(temp);
temp.clear();
}
}
for (auto iter : subsets) {
for (auto iter2 : iter) std::cout << iter2 << " ";
std::cout << std::endl;
}
for (int i = 0; i < edges.size(); i++) {
int xset = find(edges[i][0]);
int yset = find(edges[i][1]);
if (xset == yset) {
std::cout << edges[i][0] << " " << edges[i][1] << std::endl;
return edges[i];
}
setUnion(xset, yset);
}
return edge_rem;
}
};
int main() {
Solution s;
vector< vector<int> > edges;
edges.push_back({1,4});
edges.push_back({3,4});
edges.push_back({1,3});
edges.push_back({1,2});
edges.push_back({4,5});
s.findRedundantConnection(edges);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment